我在刪除包含一列中的字符串的文本文件中的行時遇到問題。到目前爲止,我的代碼無法刪除該行,但它能夠讀取文本文件並將其作爲CSV文件保存到不同的列中。但行不會被刪除。刪除一行,如果它包含CSV文件中的字符串
這是該列中的值的樣子:
Ship To or Bill To
------------------
3000000092-BILL_TO
3000000092-SHIP_TO
3000004000_SHIP_TO-INAC-EIM
而且有20多列,加50,000K行。所以基本上我試圖刪除全部包含字符串'INAC'
或'EIM'
的行。
import csv
my_file_name = "NVG.txt"
cleaned_file = "cleanNVG.csv"
remove_words = ['INAC','EIM']
with open(my_file_name, 'r', newline='') as infile, \
open(cleaned_file, 'w',newline='') as outfile:
writer = csv.writer(outfile)
for line in csv.reader(infile, delimiter='|'):
if not any(remove_word in line for remove_word in remove_words):
writer.writerow(line)
謝謝你爲我工作。 – Cesar