在 Python 中创建多个 CSV 工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15210173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating multiple CSV sheets in Python
提问by ianlokejj
Is there any way to create a CSV file with multiple sheets programmatically in Python?
有什么方法可以在 Python 中以编程方式创建包含多个工作表的 CSV 文件?
采纳答案by skeevey
Multiple CSV files. One CSV file per sheet.
多个 CSV 文件。每张纸一个 CSV 文件。
A comma seperated value file is a plain text format. It is only going to be able to represent flat data, such as a table (or a 'Sheet')
逗号分隔值文件是纯文本格式。它只能表示平面数据,例如表格(或“表格”)
For storing multiple sheets, you should use separate CSV files. You can write each one separately and import/parse them individually into their destination.
要存储多张工作表,您应该使用单独的 CSV 文件。您可以单独编写每一个并将它们单独导入/解析到它们的目的地。
回答by LonelySoul
Not sure what you are trying to do with Multiple Sheets of CSV's. Let me elaborate my thinking on this.
不确定您要对多张 CSV 文件做什么。让我详细说明一下我的想法。
- Maybe you want a folder with different CSV files in it.
- If you "can" use XML then probably you may want to have XML sheet in a big XML "Workbook".
- 也许您想要一个包含不同 CSV 文件的文件夹。
- 如果您“可以”使用 XML,那么您可能希望在大型 XML“工作簿”中包含 XML 表。
Haven't seen multiple sheets of csv yet.
还没有看到多张csv。
回答by Christopher Nuccio
The xlsxwriterlibrary is what you're looking for. It can make a workbook with multiple sheets.
Look at the link for tutorials and code.
该xlsxwriter库是你在找什么。它可以制作多张工作簿。
查看教程和代码的链接。
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.
PS 我正在回答这个问题,因为这是我在搜索如何执行此操作时得到的第一个结果。希望这对其他人有帮助。
回答by Damilola Alabi
This worked for me as a conversion from excel to CSV while exporting individual sheet names.
这对我来说是在导出单个工作表名称时从 excel 到 CSV 的转换。
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)

