0

您将使用哪些技术/模块来解析特定的字符串部分。给定类型的行:

field 1: dog        field 2: first        comment: outstanding
field 1: cat        field 2:              comment: some comment about the cat

字段名称始终以冒号结尾,字段值可以为空白,并且字段仅由空格分隔。我只想访问字段值。我知道如何使用正则表达式来做到这一点,但我确信有更优雅的方法可以用 Python 做到这一点。

4

3 回答 3

2

对我来说,这看起来像是一种固定宽度的格式。

如果是这样,你可以这样做:

data={}
ss=((0,19),(20,41),(42,80))
with open('/tmp/p.txt','r') as f:
    for n,line in enumerate(f):
        fields={}
        for i,j in ss:
            field=line[i:j]
            t=field.split(':')
            fields[t[0].strip()]=t[1].strip()
        data[n]=fields    

print data  

印刷:

{0: {'comment': 'outstanding', 'field 2': 'first', 'field 1': 'dog'}, 1: {'comment': 'some comment about the cat', 'field 2': '', 'field 1': 'cat'}}

如果你想要一个列表:

data=[]
ss=((0,19),(20,41),(42,80))
with open('/tmp/p.txt','r') as f:
    for n,line in enumerate(f):
        fields={}
        for i,j in ss:
            field=line[i:j]
            t=field.split(':')
            fields[t[0].strip()]=t[1].strip()
        data.append(fields)   

无论哪种情况,您都可以访问:

>>> data[0]['comment']
'outstanding'                 
于 2013-05-26T20:40:38.910 回答
0

像这样的东西:

>>> with open("abc") as f:
    lis = []
    for line in f:
        lis.append(dict( map(str.strip, x.split(":")) for x in line.split(" "*8)))
...         
>>> lis
[{'comment': 'outstanding', 'field 2': 'first', 'field 1': 'dog'},
 {'comment': 'some comment about the cat', 'field 2': '', 'field 1': 'cat'}
]

>>> lis[0]['comment']    #access 'comment' field on line 1
'outstanding' 
>>> lis[1]['field 2']    # access 'field 2' on line 2
''
于 2013-05-26T19:09:50.317 回答
0

另一种选择是使用csv模块。

假设字段之间有一个制表符分隔符:

import StringIO
import csv

input_data = StringIO.StringIO("""field 1: dog  field 2: first  comment: outstanding
field 1: cat    field 2:    comment: some comment about the cat""")

data = []
for row in csv.reader(input_data, delimiter="\t"):
    line = {}
    for item in row:
        value = item.split(":")
        line[value[0]] = value[1].strip()

    data.append(line)

print data

印刷

[{'comment': 'outstanding', 'field 2': 'first', 'field 1': 'dog'}, {'comment': 'some comment about the cat', 'field 2': '', 'field 1': 'cat'}]
于 2013-05-26T19:19:27.230 回答