我对python很新,我在第25列之后删除标题列时遇到问题。还有8个额外的列没有数据,所以我试图删除这些列。第1-25列有50,000k的数据,其余列是空白的。我该怎么做?我现在的代码能够清理文件,但我不能删除第25行后的行[0]的标题。
谢谢
import csv
my_file_name = "NVG.txt"
cleaned_file = "cleanNVG.csv"
remove_words = ['INAC-EIM','-INAC','TO-INAC','TO_INAC','SHIP_TO-inac','SHIP_TOINAC']
with open(my_file_name, 'r', newline='') as infile, open(cleaned_file, 'w',newline='') as outfile:
writer = csv.writer(outfile)
cr = csv.reader(infile, delimiter='|')
writer.writerow(next(cr)) #I think this is why is not working
for line in (r[0:25] for r in cr):
#del line [26:32]
if not any(remove_word in element for element in line for remove_word in remove_words):
line[11]= line[11][:5]
writer.writerow(line)
答案 0 :(得分:1)
您已找到问题所在的行 - 您只需打印所需的标题即可。 next(cr)
读取标题行,但您将整行传递给writer.writerow()
。
而不是
writer.writerow(next(cr))
你想要:
writer.writerow(next(cr)[:25])
([:25]
和[0:25]
在Python中相同)