使用python将XLSX正确转换为CSV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20105118/
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
Convert XLSX to CSV correctly using python
提问by geogeek
I am looking for a python library or any help to convert .XLSX files to .CSV files.
我正在寻找一个 python 库或任何将 .XLSX 文件转换为 .CSV 文件的帮助。
采纳答案by Hemant
Read your excel using the xlrdmodule and then you can use the csvmodule to create your own csv.
使用该xlrd模块读取您的 excel ,然后您可以使用该csv模块创建您自己的 csv。
Install the xlrd module in your command line:
在命令行中安装 xlrd 模块:
pip install xlrd
pip install xlrd
Python script:
蟒蛇脚本:
import xlrd
import csv
def csv_from_excel():
wb = xlrd.open_workbook('excel.xlsx')
sh = wb.sheet_by_name('Sheet1')
your_csv_file = open('your_csv_file.csv', 'w')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in range(sh.nrows):
wr.writerow(sh.row_values(rownum))
your_csv_file.close()
# runs the csv_from_excel function:
csv_from_excel()

