我目前正在使用xlsx writer将信息从纯文本文件传输到excel。但是,在创建excel文档之后,大多数时候我无法打开它。我收到以下错误: “我们很抱歉,但是Excel遇到了一个错误,导致它无法正常工作。因此,Excel将需要关闭。
您希望我们现在修理吗?“
然后当我选择修复时,它只是关闭Excel。有时它会起作用,我可以查看该文件。
以下是纯文本文件供参考:
"DOT_NUMBER","LEGAL_NAME","DBA_NAME","CARRIER_OPERATION","HM_FLAG","PC_FLAG","PHY_STREET","PHY_CITY","PHY_STATE","PHY_ZIP","PHY_COUNTRY","MAILING_STREET","MAILING_CITY","MAILING_STATE","MAILING_ZIP","MAILING_COUNTRY","TELEPHONE","FAX","EMAIL_ADDRESS","MCS150_DATE","MCS150_MILEAGE","MCS150_MILEAGE_YEAR","ADD_DATE","OIC_STATE","NBR_POWER_UNIT","DRIVER_TOTAL"
10000,"POWELL DISTRIBUTING CO INC","","A","N","N","9125 N BURRAGE AVE","PORTLAND","OR","97217-6961","US","P O BOX 17160","PORTLAND","OR","97217-0160","US","(503) 289-5558","(503) 735-0100","","06-DEC-16","20000","2015","01-JUN-74","OR","1","1"
1000002,"NEW JERSEY BOOM & ERECTORS INC","","A","N","N","120 SANS DRIVE","HENRYVILLE","PA","18332","US","120 SANS DRIVE","HENRYVILLE","PA","18332","US","(570) 620-1546","(570) 620-1517","GAIL@NJBOOM.COM","06-JAN-16","243647","2015","22-JAN-02","PA","1","1"
1000004,"RAY TRUCKING LLC","","C","N","N","218 PEACHTREE ST","WARWICK","GA","31796","US","PO BOX 281","WARWICK","GA","31796","US","(229) 535-4140","","MONTOYARAY@YMAIL.COM","19-OCT-16","83000","2015","22-JAN-02","GA","2","2"
1000008,"GUERRERA LANDSCAPING LLC","","C","N","N","164 NUT PLAINS RD","GUILFORD","CT","06437-2138","US","164 NUT PLAINS RD","GUILFORD","CT","06437-2138","US","(203) 453-6693","","","25-MAR-06","30000","2005","22-JAN-02","CT","4","3"
1000009,"REINHARD CORP","","C","N","N","1647 REINHARD RD","CHESHIRE","CT","06410","US","1647 REINHARD RD","CHESHIRE","CT","06410","US","(203) 272-2869","","","11-APR-06","2000","2005","22-JAN-02","CT","1","1"
1000010,"DELUXE MOTOR COMPANY INC","DELUXE HARLEY DAVIDSON-BUELL","A","N","N","3300 CONESTOGA DR","CASPER","WY","82718","US","3300 CONESTOGA DR","GILLETTE","WY","82718","US","(307) 687-2001","(307) 687-0245","JACKJR59@GMAIL.COM","26-OCT-16","50000","2014","22-JAN-02","WY","8","3"
1000012,"CENTRAL HEATING & AIR INC","","C","N","N","600 S CHARLOTTE","SIOUX FALLS","SD","57103-2614","US","600 S CHARLOTTE","SIOUX FALLS","SD","57103-2614","US","(605) 371-3076","(605) 339-4409","CENTRALHEATING@MIDCONETWORK.COM","26-OCT-10","89918","2008","22-JAN-02","SD","9","5"
1000593,"SIGNAL GATES INC","","B","N","N","1800 S 5TH AVE","TUCSON","AZ","85713-5713","US","1800 S 5TH AVE","TUCSON","AZ","85713-5713","US","(520) 628-3199","(520) 628-3620","KIM@SIGNALGATES.COM","01-NOV-13","100000","2013","24-JAN-02","AZ","7","9"
代码(更新):
import xlsxwriter
filename = "minitest.txt"
file = open(filename, 'r')
print("file opened")
workbook = xlsxwriter.Workbook("Carriers.xlsx")
worksheetA = workbook.add_worksheet()
worksheetB = workbook.add_worksheet()
worksheetC = workbook.add_worksheet()
rowA = 0
rowB = 0
rowC = 0
for line in file:
line = line.replace('"', ' ')
entry = line.split(",")
col = 0
if entry[3] == " A ":
for item in entry:
worksheetA.write(rowA, col, item)
col += 1
rowA += 1
if entry[3] == " B ":
for item in entry:
worksheetB.write(rowB, col, item)
col += 1
rowB += 1
if entry[3] == " C ":
for item in entry:
worksheetC.write(rowC, col, item)
col += 1
rowC += 1
workbook.close()
file.close()
答案 0 :(得分:1)
我没有看到你的问题。通过删除main()
和running
循环,我将您的程序简化了一下,并将row
增量移动到if语句中。如下:
import xlsxwriter
filename = "minitest.txt"
file = open(filename, 'r')
print("file opened")
workbook = xlsxwriter.Workbook("Carriers.xlsx")
worksheetA = workbook.add_worksheet()
row = 0
for line in file:
line = line.replace('"', ' ')
entry = line.split(",")
col = 0
if entry[3] == " A ":
print(entry[3])
for item in entry:
print(row, "|", col)
print(item)
worksheetA.write(row, col, item)
col += 1
row += 1
workbook.close()
file.close()
然后我用你的示例文本文件运行它,它产生预期的输出而没有警告:
更新:我运行了更新的程序,但没有看到任何问题: