python:xlsx转csv文件

代码:

import pandas as pd
def xlsx_to_csv_pd():
    data_xls = pd.read_excel('Test_labels.xlsx',index_col=0)
data_xls.to_csv('Test_labels.csv')
if __name__ == '__main__':
    xlsx_to_csv_pd()

结果:
python:xlsx转csv文件_第1张图片
第一行文件有问题

用下面的代码就是好的,不知道上面的错误原因

# pip install xlrd==1.2.0
import xlrd
import csv
import codecs

def xlsx_to_csv():
    workbook = xlrd.open_workbook('Test_labels.xlsx')
    table = workbook.sheet_by_index(0)
    with codecs.open('Test_labels.csv', 'w', encoding='utf-8') as f:
        write = csv.writer(f)
        for row_num in range(table.nrows):
            row_value = table.row_values(row_num)
            write.writerow(row_value)

if __name__ == '__main__':
    xlsx_to_csv()

你可能感兴趣的:(python,pandas,数据分析)