我有一个包含多个行和列的csv文件。 我想使用python 3.4过滤我的文件并创建一个与行中某些值完全匹配的新文件。我的问题是我选择2列来过滤行:如果EQA nb与列表中组织的确切名称匹配,则将该行复制到新文件。
我的文件看起来像那样:
EQA_nb,organization,a,b,c,d
16,TCS,a1,b1,c1,d1
14,LS,a2,b2,c2,d2
14,BMC,a3,b3,c3,d3
2,TLS,a4,b4,c4,d4
6,LCHC,a5,b5,c5,d5
14,TLS,a6,b6,c6,d6
etc..
我的代码是:
import csv
with open ('data.csv','w',newline='') as outf, open('output.csv','r', newline='')as inf:
reader = csv.DictReader(inf, delimiter=',')
writer = csv.DictWriter(outf, fieldnames=reader.fieldnames, delimiter=',')
writer.writeheader()
for row in reader:
if ((row["EQA_nb"]== '6' and row["organization"] in "EP, LCHC, SP")
or (row["EQA_nb"]== "2" and row["organization"] in "AMC, LCHC, SGPLP, TCS, TLS")
or (row["EQA_nb"]== "7" and row["organization"] in "GP")
or (row["EQA_nb"]== "8" and row["organization"] in "LFMC")
or (row["EQA_nb"]== "9" and row["organization"] in "LS")
or (row["EQA_nb"]== "11" and row["organization"] in "DP, WSS")
or (row["EQA_nb"]== "13" and row["organization"] in "TRP")
or (row["EQA_nb"]== "14" and row["organization"] in "BMC,TLS")):
writer.writerow(row)
我想让新文件显示指定行值的精确匹配,即显示14 TLS但不是14 LS,例如当前代码所示。 谢谢你的帮助!
答案 0 :(得分:0)
替换
'BMC,TLS'
带
['BMP', 'TLS']
然后
row['organization'] in ['BMP', 'TLS']
将匹配' TLS'但不是' LS'
答案 1 :(得分:0)
通过将匹配项放入字典中,将if / elif链简化为单个查找,可以很整洁地完成此操作,然后可以使用operator.itemgetter对其进行优化。
import csv
import operator
mapping = {
"6": ["EP", "LCHC", "SP"],
"2": ["AMC", "LCHC", "SGPLP", "TCS", "TLS"],
"7": ["GP"],
"8": ["LFMC"],
"9": ["LS"],
"11": ["DP", "WSS"],
"13": ["TRP"],
"14": ["BMC", "TLS"],
}
kv = operator.itemgetter("EQA_nb", "organization")
with open("data.csv", "w", newline="") as outf, open("output.csv", "r", newline="") as inf:
reader = csv.DictReader(inf, delimiter=",")
writer = csv.DictWriter(outf, fieldnames=reader.fieldnames, delimiter=",")
writer.writeheader()
for row in reader:
eqa, org = kv(row)
if org in mapping.get(eqa, []):
writer.writerow(row)