I have a file that is really in csv format but is being handed to me as a .txt. I need to take this file and transfer it to .xls format so I can upload it into Google Docs. Currently I have the code below but it is spitting out the file with everything on the line into the first cell instead of breaking out different cells for every comma value. Does anyone know how to solve this?
Thanks!
import os
fin = open(r"C:\Users\JohnDoe\Downloads\Report.txt", "r")
data_list = fin.readlines()
fin.close() # closes file
fout = open(r"C:\Users\JohnDoe\Desktop\Report.xls", "w")
fout.writelines(data_list)
fout.flush()
fout.close()
Just renaming it to
.xlsdoesn’t convert it to an Excel spreadsheet. But regardless, why would you convert it to.xlsjust to upload it to Google Docs?If it’s a
.csv, just rename the file to.csvand upload it and it’ll properly be detected as a spreadsheet.Use
os.rename:if you really need to do it with Python.
If you need to get actual rows from the file instead of lines:
will give you a list of cells for each line.
If you actually do need to work with an Excel spreadsheet, see http://www.python-excel.org/ and the xlwt package.