Get CSV data with Python, format, paste Excel I don't think there is such a situation, but unfortunately I was there.
2016/02/14 postscript @Okadate left an ultra-stylish technique in the comments. Please refer to that. After that, it will be the wreckage that was created by the tension at midnight.
Windows7 Python3.4
sample.csv
,Label,Area,Mean,Min,Max
1,4mas.tif,40000,0.000,0,0
2,10mas.tif,40000,32757.335,30635,34880
3,25mas.tif,40000,32784.505,1039,64761
4,40mas.tif,40000,32836.377,1371,65157
If you copy the above CSV and paste the value into Excel It will be like this.
The ideal is
sample.py
import win32com.client as win32
xlApp = win32.Dispatch("Excel.Application")
xlApp.Visible = 1
wb = xlApp.Workbooks.Open("C:/Users/user/Documents/1.xls")
Sheet = xlApp.Workbooks(1).Sheets(1)
f=open("C:/Users/user/Documents/sample.csv",'r')
for i,line in enumerate(f.readlines()):#This is miso
for n,label in enumerate(line.split(",")):
cell = Sheet.Cells(i+1,n+1)#In win32, to start Cell from 1
cell.Value = label
That is the miso. Since enumerate loops with the index, add +1 to it to match the Cell. It's a Python-like way of writing (I just want to say)
It would be a moment if I did it by hand, but I wanted to do it in Python. (I came up with it in the middle of the night, so I feel like I overlooked something important)
Please let me know if there is another good way
problem. .. .. It will be something like "wrap around and display the whole" without permission. It's not good.
Recommended Posts