-3
我想從我的桌面上讀取文本文件,並使用Python將其寫入Excel文件。Python:文本文件到Excel文件
我得到的錯誤:
Traceback (most recent call last):
File "C:\Users\Kenny\Desktop\TTUMExcel.py", line 26, in <module>
worksheet.write(row, col + 1, split_Line[1])
IndexError: list index out of range
這裏是我的代碼:
import os
import re
import xlsxwriter
path = 'C:\Users\Kenny\Desktop\TTUM 2'
listing = os.listdir(path)
workbook = xlsxwriter.Workbook("Bank Validation.xlsx")
bold = workbook.add_format({'bold' : True})
for infile in listing:
dir_item_path = os.path.join(path, infile)
fh = open(dir_item_path,'r')
Fname = infile
Lname = Fname.split('.')[0]
worksheet = workbook.add_worksheet(Lname)
col = 0
row = 0
worksheet.write('A1', 'ACCOUNT', bold)
worksheet.write('B1', 'COUNTRY', bold)
worksheet.write('C1', 'DIGITS', bold)
worksheet.write('D1', 'CORRECTIONBILLSTUFF', bold)
for line in fh:
space_remove = re.sub(r"\s+",",",line.rstrip())
#Check for third argument of sub
split_Line = space_remove.split(" ")
worksheet.write(row, col, split_Line[0])
worksheet.write(row, col + 1, split_Line[1])
worksheet.write(row, col + 2, split_Line[2])
worksheet.write(row, col + 3, split_Line[3])
row += 1
workbook.close()
你還可以發佈完整的錯誤日誌嗎? – sk11
發佈錯誤日誌以查看哪些列表超出範圍? – Robert
您確定該路徑不包含任何目錄嗎?如果它包含任何目錄,那麼'Lname = Fname.split('。')[0]'會報錯。 – sk11