我有一个包含大量列的csv文件,而某些列的行数比其他列多。我试图读取某些列并将这些列提取到不同的csv文件。但是,我继续使列表索引超出范围,因为我正在读取的列比csv文件中的其他列短。
import csv
import os
import os.path
'Open csv directory to extract data from'
path = 'S://Raw Data/VehicleData/'
'Get file names and print number of files in the directory'
files = [f for f in os.listdir(path)]
print(files)
print(len(files))
'Start loop to scan through files in directory'
for j in range(len(files)):
'determine name of file and the output name of the file'
name = files[j]
print(name)
'path to file to open'
fpath = os.path.join(path, files[j])
if files[j].endswith(".csv"):
nameout = 'VehicleN3STPData' + '.csv'
with open (fpath, 'r', newline='') as csvfile:
testcsv = csv.reader(csvfile,delimiter=',')
with open (nameout,'a+',newline='') as outfile:
writer = csv.writer(outfile)
for row in testcsv:
my_row = []
my_row.append(row[59])
my_row.append(row[111])
my_row.append(row[120])
print(my_row)
writer.writerow(my_row)
outfile.close()
csvfile.close()
答案 0 :(得分:1)
你需要一个条件陈述来检查长度,或者你需要一个尝试&除了条款取决于您的预期行为。
以下是使用条件语句的示例,如果它们不存在,则将列值保留为空白。
而不是:
my_row.append(row[59])
my_row.append(row[111])
my_row.append(row[120])
执行:
for col_num in [59, 111, 120]:
my_row.append(row[col_num] if len(row) > col_num else '')
答案 1 :(得分:0)
感谢您的帮助,这里是最终的代码,使用islice删除标头,使用条件语句,然后删除空白行if if(my_row)thanks !!
if files[j].endswith(".csv"):
nameout = 'VehicleN3STPData' + '.csv'
with open (fpath, 'r', newline='') as csvfile:
testcsv = csv.reader(csvfile,delimiter=',')
with open (nameout,'a+',newline='') as outfile:
writer = csv.writer(outfile)
for row in islice(testcsv,3,None):
my_row = []
for col_num in [59,111,120]:
my_row.append(row[col_num] if len(row) > col_num else '')
if any(my_row):
writer.writerow(my_row)
outfile.close()
csvfile.close()enter code here