如果在Python中为具有相同id的行找到匹配项,则从文件中获取值
我有一个包含行数据的文件。每行以一个id开头,后面是一组固定的属性,属性之间用逗号分隔如果在Python中为具有相同id的行找到匹配项,则从文件中获取值,python,for-loop,file-io,readlines,Python,For Loop,File Io,Readlines,我有一个包含行数据的文件。每行以一个id开头,后面是一组固定的属性,属性之间用逗号分隔 123,2,kent,..., 123,2,bob,..., 123,2,sarah,..., 123,8,may,..., 154,4,sheila,..., 154,4,jeff,..., 175,3,bob,..., 249,2,jack,..., 249,5,bob,..., 249,3,rose,..., 如果满足条件,我想获得一个属性。条件是,如果“bob”出现在同一id中,则获取后面第二
123,2,kent,...,
123,2,bob,...,
123,2,sarah,...,
123,8,may,...,
154,4,sheila,...,
154,4,jeff,...,
175,3,bob,...,
249,2,jack,...,
249,5,bob,...,
249,3,rose,...,
如果满足条件,我想获得一个属性。条件是,如果“bob”出现在同一id中,则获取后面第二个属性的值
For example:
id: 123
values returned: 2, 8
id: 249
values returned: 3
Java有一个我可以使用的双循环,但我想在Python中尝试一下。任何建议都很好。只要在循环过程中设置一个标志或其他东西:
name = 'bob'
id = '123'
found = False
for line in file:
l = line.split(',')
if l[0] == id:
if l[2] == name:
found = True
if found:
print l[1]
结果:
>>> dd
defaultdict(<type 'list'>, {'175': [], '123': ['2', '8'], '249': ['3']})
>>dd
defaultdict(,{'175':[],'123':['2','8'],'249':['3']})
我想出了一个(也许)更具Python风格的解决方案,它使用和。此方法产生的结果与下面的方法相同,但我认为它更漂亮:)旗帜、“curr_id”之类的东西不是很像蟒蛇,如果可能的话应该避免
import csv
from itertools import groupby, dropwhile
goal = 'bob'
ids = {}
with open('my_data.csv') as ifile:
reader = csv.reader(ifile)
for key, rows in groupby(reader, key=lambda r: r[0]):
matched_rows = list(dropwhile(lambda r: r[2] != goal, rows))
if len(matched_rows) > 1:
ids[key] = [row[1] for row in matched_rows[1:]]
print ids
(下面是第一个解决方案) 输出:
{'123': ['2', '8'], '249': ['3']}
为什么id 249的
返回值是3
而不是2,5,3
?+1ah这比我的答案要好,我的答案类似,但是它有一个空的175@bernie谢谢,伙计,但我对这个解决方案不满意-我不喜欢使用标志、curr_id和其他东西…:)@伯尼:如果你感兴趣的话,我找到了另一个解决办法:)非常干净。利用已按ID排序的数据。感谢Steinar Lima的回答!它的工作原理完全符合我的要求,并且你能够发现我试图读取的文件实际上是一个csv。
from collections import defaultdict
import csv
curr_id = None
found = False
goal = 'bob'
ids = defaultdict(list)
with open('my_data.csv') as ifile:
for row in csv.reader(ifile):
if row[0] != curr_id:
found = False
curr_id = row[0]
if found:
ids[curr_id].append(row[1])
elif row[2] == goal:
found = True
print dict(ids)
{'123': ['2', '8'], '249': ['3']}