在文件中查找一行,然后在Python中读取下几行

时间:2012-11-11 15:05:47

标签: python

我有一个包含以下数据的纯文本文件:

id=1
name=Scott
occupation=Truck driver
age=23

id=2
name=Dave
occupation=Waiter
age=16

id=3
name=Susan
occupation=Computer programmer
age=29

我正在尝试找出给定id字符串的文件中任意点的最佳方法,然后抓取下面的行以提取数据以供我的程序使用。我可以这样做:

def get_person_by_id(id):
    file = open('rooms', 'r')
    for line in file:
        if ("id=" + id) in line:
            print(id + " found")

但是我不知道我现在怎么能通过下一行来做line.split("=")或类似的提取我可以使用我的程序的信息(放入列表或字典或其他)。有什么指针吗?

7 个答案:

答案 0 :(得分:2)

一种选择是将整个内容加载到内存中,这样可以避免每次都读取文件:

with open('rooms') as f:
    chunks = f.read().split('\n\n')

people_by_id = {}

for chunk in chunks:
    data = dict(row.split('=', 1) for row in chunk.split('\n'))
    people_by_id[data['id']] = data
    del data['id']

def get_person_by_id(id):
    return people_by_id.get(id)

答案 1 :(得分:0)

找到正确的行后退出for循环如何:

def get_person_by_id(id):
    file = open('rooms', 'r')
    for line in file:
        if ("id=" + id) in line:
            print(id + " found")
            break
    #now you can continue processing your file:
    next_line = file.readline()

答案 2 :(得分:0)

也许:

d = dict()

with open(filename) as f:
    for line in f:
        k,v = line.split('=')
        if 'id=' in line:
            d[v] = {}
        d[d.keys()[-1]][k] = v

答案 3 :(得分:0)

获取所有人的属性和值( id,姓名,职业,年龄等),直到找到 一个强大的路线。

def get_person_by_id(id):
    person = {}
    file = open('rooms', 'r')
    for line in file:
        if found == True:
            if line.strip():
                attr, value = line.split("="):
            else:
                return person              
        elif ("id=" + id) in line:
            print(id + " found")
            found = True
            attr, value = line.split("=")
            person[attr] = value
    return person

答案 4 :(得分:0)

这是一个迭代解决方案。

objects = []
current_object = None
with open("info.txt", "rb") as f:
    for line in f:
        line = line.strip("\r\n")
        if not line:
            current_object = None
            continue
        if current_object is None:
            current_object = {}
            objects.append(current_object)
        key,_,value = line.partition('=')
        current_object[key] = value

print objects

答案 5 :(得分:0)

迭代解析器的另一个例子:

from itertools import takewhile
def entries(f):
    e = {}
    def read_one():
        one = {}
        for line in takewhile(lambda x: '=' in x, f):
            key, val = line.strip().split('=')
            one[key] = val
        return one
    while True:
        one = read_one() 
        if not one:
            break
        else:
            e[one.pop('id')] = one
    return e

示例:

>>> with open('data.txt') as f:
..:    print entries(f)['2']
{'age': '16', 'occupation': 'Waiter', 'name': 'Dave'}

答案 6 :(得分:0)

此解决方案对记录中的空行更加宽容。

def read_persons(it):
    person = dict()
    for l in it:
        try:
            k, v = l.strip('\n').split('=', 1)
        except ValueError:
            pass
        else:
            if k == 'id': # New record
                if person:
                    yield person
                    person = dict()
            person[k] = v
    if person:
        yield person