[英]Removing lines in CSV file is adding extra lines
我正在處理一項編碼任務,其中應用程序的要求之一是能夠刪除 CSV 文件中感興趣的行。 當我嘗試刪除由鍵(名稱)標識的行時,它不僅會刪除該行,還會將我的第一行的多個副本添加到我的 CSV 文件中。 我似乎無法弄清楚為什么要添加這些重復的行。 任何幫助表示贊賞。
供參考:景點是csv文件復制到的字典列表
刪除 function 如下
name = entername()
with open('boston.csv', 'r') as csv_read:
reader = csv.reader(csv_read)
for row in reader:
attractions.append(row)
for field in row:
if field == name:
attractions.remove(row)
with open('boston.csv', 'w') as csv_write:
writer = csv.writer(csv_write)
writer.writerows(attractions)
而我之前的 CSV 文件看起來像這樣:
Short Name,Name,Category,URL,Lat,Lon,Color
harvard,Harvard University,university,https://www.harvard.edu/,42.373032,-71.116661,green
mit,Massachusetts Institute of Technology,University,https://www.mit.edu/,42.360092,-71.094162,green
science,Museum of Science,Tourism,https://www.mos.org/,42.36932,-71.07151,green
children,Boston Children's Museum,Tourism,https://bostonchildrensmuseum.org/,42.3531,-71.04998,green
但結果是:
Short Name,Name,Category,URL,Lat,Lon,Color
Short Name,Name,Category,URL,Lat,Lon,Color
Short Name,Name,Category,URL,Lat,Lon,Color
Short Name,Name,Category,URL,Lat,Lon,Color
Short Name,Name,Category,URL,Lat,Lon,Color
harvard,Harvard University,university,https://www.harvard.edu/,42.373032,-71.116661,green
science,Museum of Science,Tourism,https://www.mos.org/,42.36932,-71.07151,green
children,Boston Children's Museum,Tourism,https://bostonchildrensmuseum.org/,42.3531,-71.04998,green
我已經運行了你的代碼,它似乎可以工作。
我將其修改為不覆蓋輸入文件(這在調試時非常有用),在刪除一行時打印一條消息,並對名稱進行硬編碼(同樣,僅用於調試):
import csv
name = 'Harvard University'
attractions = []
with open('boston.csv', 'r') as csv_read:
reader = csv.reader(csv_read)
for row in reader:
attractions.append(row)
for field in row:
if field == name:
print(f'{field} matches {name}, removing {row}')
attractions.remove(row)
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(attractions)
當我運行它時,我看到這些調試打印消息:
Harvard University matches Harvard University, removing ['harvard', 'Harvard University', 'university', 'https://www.harvard.edu/', '42.373032', '-71.116661', 'green']
這是我的output.csv :
Short Name,Name,Category,URL,Lat,Lon,Color
mit,Massachusetts Institute of Technology,University,https://www.mit.edu/,42.360092,-71.094162,green
science,Museum of Science,Tourism,https://www.mos.org/,42.36932,-71.07151,green
children,Boston Children's Museum,Tourism,https://bostonchildrensmuseum.org/,42.3531,-71.04998,green
當我將 name 更改為name = 'Tourism'
時,這對您的邏輯有效(即使它不是您想要的/不打算的),它仍然會按照您的預期進行,刪除Tourism在Category中的兩行場地:
...
name = 'Tourism'
attractions = []
...
Tourism matches Tourism, removing ['science', 'Museum of Science', 'Tourism', 'https://www.mos.org/', '42.36932', '-71.07151', 'green']
Tourism matches Tourism, removing ['children', "Boston Children's Museum", 'Tourism', 'https://bostonchildrensmuseum.org/', '42.3531', '-71.04998', 'green']
Short Name,Name,Category,URL,Lat,Lon,Color
harvard,Harvard University,university,https://www.harvard.edu/,42.373032,-71.116661,green
mit,Massachusetts Institute of Technology,University,https://www.mit.edu/,42.360092,-71.094162,green
有一個純 python convtools庫,它在后台生成代碼並提供大量數據處理原語:
from convtools import conversion as c
from convtools.contrib.tables import Table
name = entername()
table = Table.from_csv("boston.csv") # pass header=True if it's there
columns = table.columns
table.filter(
c.not_(
c.or_(*(c.col(column_name) == name for column_name in columns))
if len(columns) > 1
else c.col(columns[0]) == name
)
).into_csv("boston_output.csv")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.