Python uses the xlwt library to write files with a limit of 65535 lines to solve

1. Encountered a problem:
errorlog: ValueError: row index was 65536, not allowed by .xls format
Write to line 65535 and report an error
2. The cause of the problem:
The xlwt library limits the number of rows written to 65535

3. Solution:
The xlsxwriter library replaces the xlsx library. After testing, it can write 1 million+ lines without limit.
Test instance:

import xlsxwriter

file1 = 'test.txt'      ##可以放100万多列的测试数据,以|为分割符
# 创建工作簿
workbook = xlsxwriter.Workbook('test-2022-07-11.xlsx')  # 创建一个excel文件

# 创建工作表
worksheet1 = workbook.add_worksheet('netdata')  # 在文件中创建一个名为test-sheet1的sheet,不加名字默认为sheet1
row = 0
with open(file1, 'r') as f:
    for i in f.readlines():
        i = i.strip().split('|')
        #public100(i[3])
        wdata = i
        # print(wdata)
        for i in range(len(wdata)):
            worksheet1.write(row,i,str(wdata[i]) )  # 行,列,写入单元格的数据
        row += 1
        print(row)

workbook.close()   # 关闭写

Guess you like

Origin blog.csdn.net/u013908944/article/details/125763460