0
我是新的python,並嘗試修改csv文件,所以我將能夠根據給定的列表刪除特定字段的特定字段。 在我當前的代碼中,我得到了我想要刪除的行,但我無法刪除它並將更改保存在同一文件(替換)中。如何刪除csv文件中的整個行並將更改保存在同一文件中?
import os, sys, glob
import time ,csv
# Open a file
path = 'C:\\Users\\tzahi.k\\Desktop\\netzer\\'
dirs = os.listdir(path)
fileslst = []
alertsCode = ("42001", "42003", "42006","51001" , "51002" ,"61001" ,"61002","71001",
"71002","71003","71004","71005","71006","72001","72002","72003","72004",
"82001","82002","82003","82004","82005","82006","82007","83001","84001")
# This would print the unnesscery codes
for file in dirs:
if "ALERTS" in file.upper() :
fileslst.append(file)
fileslst.sort()
with open(fileslst[-1], 'rb') as csvfile:
csvReader = csv.reader(csvfile)
for row in csvReader:
for alert in alertsCode:
if any(alert in row[2] for s in alertsCode) :
print row
有幫助嗎?
是有閱讀的理由和二進制寫(''rb'', ''wb'')模式? –
@ Ev.Kounis特別沒有理由。只有跟隨OP的優先級 –
工作的很棒! 謝謝,我很感激它 –