I want to convert a xlsx-file
to .csv
. I tried it with this function:
def excel_to_csv(excel_file, csv_file):
# read Excel file into pandas dataframe
df = pd.read_excel(excel_file)
# write dataframe to CSV file
df.to_csv(csv_file, index=False, header=False)
This is my Excel-file:
It is always two lines.
But when running the function above it removes the first line of my Excel-file.
That happens because I set header=False
. However if I don't set that it places random 1
in my csv
file.
As workaround I could add an empty first line, but is there a better solution for this?
Pandas is reading the first row of the xlsx as the header and then discarding it. Instead, read the xlsx as:
df = pd.read_excel(excel_file, header=None)
Including header=None
assumes there is no header in the xlsx file. The default is header=0
, which assumes the first row is the header.