简体   繁体   English

将 txt 行块转换为字典列表 python

[英]Converting block of txt lines to list of dictionaries python

I have a generic txt made like this:我有一个这样的通用txt

name: John
surname: 'Doe'
company: 'Municipality'
email: john@doe.com

name: John2
surname: 'Doe2'
company: 'Municipality2'
email: john2@doe2.com

name: John3
surname: 'Doe3'
company: 'Municipality3'
email: john3@doe3.com

name: John4
surname: 'Doe4'
company: 'Municipality4'
email: john4@doe4.com

with empty lines dividing each person .用空线分隔每个 What is the best method to covert the file as a list of dictionaries?将文件转换为字典列表的最佳方法是什么? Each dictionary is the single person.每本词典都是一个人。

You could just split on \n\n and then do line,你可以在\n\nsplit然后做线,

>>> l = []
>>> for block in f.read().strip().split('\n\n'):
...   d = {}
...   for line in block.split('\n'):
...     if not line.strip():continue
...     key, value = line.strip().split(':')
...     d[key.strip()] = value.strip("' ") # strip the `'` and/or `space` at both ends in `value`
...   l.append(d)
... 
>>> import pprint
>>> pprint.pprint(l)
[{'company': 'Municipality',
  'email': 'john@doe.com',
  'name': 'John',
  'surname': 'Doe'},
 {'company': 'Municipality2',
  'email': 'john2@doe2.com',
  'name': 'John2',
  'surname': 'Doe2'},
 {'company': 'Municipality3',
  'email': 'john3@doe3.com',
  'name': 'John3',
  'surname': 'Doe3'},
 {'company': 'Municipality4',
  'email': 'john4@doe4.com',
  'name': 'John4',
  'surname': 'Doe4'}]

You can do something like this:你可以这样做:

def convert_text(txt):
    dct = {}
    for line in txt.splitlines():
        if not line:  # Blank line
            yield dct
            dct = {}
        else:
            key, value = line.split(': ', 1)
            dct[key] = value
    yield dct
 result = []

 for each in data.split('\n\n'):
     d = {}
     for line in filter(None, each.split('\n')):
         k, v = line.split(':')
         d[k.strip()] = v.strip("' ")
     result.append(d)

Which results in:结果是:

In [95]: result
Out[95]:
[{'name': 'John',
  'surname': 'Doe',
  'company': 'Municipality',
  'email': 'john@doe.com'},
 {'name': 'John2',
  'surname': 'Doe2',
  'company': 'Municipality2',
  'email': 'john2@doe2.com'},
 {'name': 'John3',
  'surname': 'Doe3',
  'company': 'Municipality3',
  'email': 'john3@doe3.com'},
 {'name': 'John4',
  'surname': 'Doe4',
  'company': 'Municipality4',
  'email': 'john4@doe4.com'}]

You can try this with itertools.groupby :你可以用itertools.groupby试试这个:

from itertools import groupby

with open('filename.txt','r') as f:
    lines = [l.strip('\n') for l in f]
    groups = groupby(lines,key=bool)
    final_list = [{val.split(':')[0]:val.split(':')[1].strip("' ") for val in g} for k,g in groups if k]

final_list:最终清单:

[{'company': 'Municipality',
  'email': 'john@doe.com',
  'name': 'John',
  'surname': 'Doe'},
 {'company': 'Municipality2',
  'email': 'john2@doe2.com',
  'name': 'John2',
  'surname': 'Doe2'},
 {'company': 'Municipality3',
  'email': 'john3@doe3.com',
  'name': 'John3',
  'surname': 'Doe3'},
 {'company': 'Municipality4',
  'email': 'john4@doe4.com',
  'name': 'John4',
  'surname': 'Doe4'}]

Try this,尝试这个,

final_list = []
with open('file1.txt', 'r') as file:
    _temp = {}
    for line in file:
        if line !='\n':
            _temp.setdefault(line.split(':')[0].strip(), line.split(':')[1].replace('\n', '').strip())
        else:
            final_list.append(_temp)
            _temp = {}

print(final_list)

Output: Output:

[{'company': "'Municipality'",
  'email': 'john@doe.com',
  'name': 'John',
  'surname': "'Doe'"},
 {'company': "'Municipality2'",
  'email': 'john2@doe2.com',
  'name': 'John2',
  'surname': "'Doe2'"},
 {'company': "'Municipality3'",
  'email': 'john3@doe3.com',
  'name': 'John3',
  'surname': "'Doe3'"}]

Using Regex.使用正则表达式。

Ex:前任:

result = [{}]
with open(filename) as infile:
    for line in infile:            #Iterate each line
        line = line.strip()        #Check for empty line
        if line:
            key, value = re.match(r"(\w+):\s*'?(\w+)'?", line.strip()).groups()  #Get key-value pair
            result[-1][key] = value
        else:
            result.append({})
print(result)

Output: Output:

[{'company': 'Municipality', 'email': 'john', 'name': 'John', 'surname': 'Doe'},
 {'company': 'Municipality2',
  'email': 'john2',
  'name': 'John2',
  'surname': 'Doe2'},
 {'company': 'Municipality3',
  'email': 'john3',
  'name': 'John3',
  'surname': 'Doe3'},
 {'company': 'Municipality4',
  'email': 'john4',
  'name': 'John4',
  'surname': 'Doe4'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM