1

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()
3
  • 1
    It's not clear from your question why you're converting it to xls as Google Docs can certainly handle a .csv. If my assumption that you don't actually need a .xls is wrong, let me know. Commented Nov 10, 2011 at 3:36
  • 1
    Renaming the file to YourOriginalFileName.csv and then opening it in Excel and save it as xls file. Will this not achieve what you want? Or are you looking for ways to automate it? Commented Nov 10, 2011 at 3:44
  • @yasouser Who says he has a copy of Excel? What if it needs to be automated? Commented Nov 10, 2011 at 3:52

3 Answers 3

6

Just renaming it to .xls doesn't convert it to an Excel spreadsheet. But regardless, why would you convert it to .xls just to upload it to Google Docs?

If it's a .csv, just rename the file to .csv and upload it and it'll properly be detected as a spreadsheet.

Use os.rename:

import os

os.rename("C:\Users\JohnDoe\Downloads\Report.txt", 
          "C:\Users\JohnDoe\Downloads\Report.cvs")

if you really need to do it with Python.

If you need to get actual rows from the file instead of lines:

from csv import reader

for row in reader(open(r"C:\Users\JohnDoe\Downloads\Report.txt", 'rb')):
    # do something with row

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Your code is simply copying Report.txt into Report.xls

When opening Report.xls Excel is recognising it as a csv file, however it is not recognising the commas as delimiters.

You will need to use some kind of library to write proper Excel files. Look at Word's COM interface for example.

2 Comments

"recognising it as a csv file, however it is not recognising the commas as delimiters." doesn't make any sense. Either it knows it's a .csv file, in which case it knows the commas are delimiters, or it doesn't know it's a .csv file.
Excel could recognise it as a delimiter separated values but be expecting a tab delimiter or other delimiter. At least Openoffice remembers the delimiter that was selected last time or autodetects the delimieter. I do not use Excel itself but I assume it does something similar.
0

Try http://packages.python.org/openpyxl/ or for older excel files, check out this help page using pyExcelerator.

1 Comment

pyExcelerator is extinct. xlwt is a fork of pyExcelerator with bug fixes and enhancements. See the answer by @agf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.