0

I currently export reports from a website as a 'csv' file. But, when I open them I get this pop up message:

The file you are trying to open, 'Jul2015.csv', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?

I click 'yes' and the file opens in some sort of 'xls' format. So I save as a 'csv' format and when I report the newly saved file it follows the 'csv' format.

So my question is, how can I in python open the initial exported report and save as a 'csv' so I can avoid the inconsistent formatting?

Thanks

collarblind
  • 4,549
  • 13
  • 31
  • 49
  • possible duplicate of [xls to csv convertor](http://stackoverflow.com/questions/9884353/xls-to-csv-convertor) – taesu Sep 16 '15 at 13:34

2 Answers2

2

try:

import pandas as pd
data_xls = pd.read_excel('export.csv', 'Sheet1', index_col=None)
data_xls.to_csv('exceltocsv.csv', encoding='utf-8')
taesu
  • 4,482
  • 4
  • 23
  • 41
  • FYI, this doesn't work if the file won't read in via read_excel because it's a XML spreadsheet masquerading as a .xls file. – Korzak Nov 27 '18 at 01:54
0

this should work too, and doesn't use an external library.

with open("exported.csv", "r") as f:
    lines = f.readlines()
with open("xlstocsv.csv", "r") as g:
    g.writelines(lines)
Joe Smart
  • 751
  • 3
  • 10
  • 28