Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating multiple CSV sheets in Python

Tags:

python

csv

Is there any way to create a CSV file with multiple sheets programmatically in Python?

like image 918
ianlokejj Avatar asked Mar 04 '13 20:03

ianlokejj


2 Answers

The xlsxwriter library is what you're looking for. It can make a workbook with multiple sheets.
Look at the link for tutorials and code.

P.S I am answering this because this is the first result I got when searching on how to do this. Hopefully this helps others.

like image 57
Christopher Nuccio Avatar answered Oct 28 '22 07:10

Christopher Nuccio


This worked for me as a conversion from excel to CSV while exporting individual sheet names.

    xls = pd.read_excel/csv('file name.xls/csv', sheet_name =['1','2','3','4','5','6','7','8','9','10'])

    #list out the sheets  you want to export in the bracket above separated by commas and ' '

    for sheet_name, df in xls.items():
     df['sheets'] = sheet_name    
     df[['sheets']].to_csv(f'{sheet_name}.csv', header=None)
    export_csv = df.to_csv (r'Location you want to export to on your machine',
                            index = None, header=True)
like image 29
Damilola Alabi Avatar answered Oct 28 '22 08:10

Damilola Alabi