[英]How to copy header row to new csv in python
我似乎無法弄清楚如何將主行的標題行復制到匹配的行中……我需要在主csv中抓取第一行並將其寫入匹配行中,如果符合條件,則寫其余行。 ..
with open('master.csv', 'r') as master, open('match.csv', 'w') as matched:
for line in master:
if any(city in line.split('","')[5] for city in citys) and \
any(state in line.split('","')[6] for state in states) and \
not any(category in line.split('","')[2] for category in categorys):
matched.write(line)
請幫忙。 我是python的新手,不知道如何使用熊貓或其他任何東西...
您可以只消耗文件的第一行來進行讀取,然后將其寫回到要寫入的文件中:
with open('master.csv', 'r') as master, open('match.csv', 'w') as matched:
matched.write(next(master)) # can't use readline when iterating on the file afterwards
不過,看來其余部分確實需要csv
模塊。 我將編輯答案以嘗試朝該方向嘗試
使用csv
模塊,不需要那些不安全的split
。 逗號是默認的分隔符,引號也可以正確處理。 所以我只寫:
import csv
with open('master.csv', 'r') as master, open('match.csv', 'w') as matched:
cr = csv.reader(master)
cw = csv.writer(matched)
cw.writerow(next(cr)) # copy title
for row in cr: # iterate on the rows, already organized as lists
if any(city in row[5] for city in citys) and \
any(state in row[6] for state in states) and \
not any(category in row[2] for category in categorys):
cw.writerow(row)
順便說一句,您的過濾器會檢查row[5]
是否包含city
,但也許您想要完全匹配。 例如: "York"
將匹配"New York"
,這可能不是您想要的。 所以我的建議是使用in
檢查每個條件的字符串是否在字符串列表中:
import csv
with open('master.csv', 'r') as master, open('match.csv', 'w') as matched:
cr = csv.reader(master)
cw = csv.writer(matched)
cw.writerow(next(cr)) # copy title
for row in cr:
if row[5] in citys and row[6] in states and not row[2] in categorys:
cw.writerow(row)
使用生成器理解甚至可以一次寫入所有行,這甚至可以更好:
import csv
with open('master.csv', 'r') as master, open('match.csv', 'w') as matched:
cr = csv.reader(master)
cw = csv.writer(matched)
cw.writerow(next(cr)) # copy title
cw.writerows(row for row in cr if row[5] in citys and row[6] in states and not row[2] in categorys)
請注意, citys
, states
和categorys
最好使用set
而不是list
因此查找算法要快得多(您未提供該信息)
如果您不想過分考慮行產生迭代器的工作原理,那么一種簡單的方法是將第一行視為特殊:
with open('master.csv', 'r') as master, open('match.csv', 'w') as matched:
first_line = True
for line in master:
if first_line or (any(city in line.split('","')[5] for city in citys) and \
any(state in line.split('","')[6] for state in states) and \
not any(category in line.split('","')[2] for category in categorys)):
matched.write(line)
first_line = False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.