我正在使用python 2.7,并使用python dict
s。
我有这样的输出:
goods: apples, oranges
trunk_names: trunk01, trunk02, trunk03,trunk04,
trunk05,trunk06, trunk07,trunk08,
trunk09,trunk10, trunk11,trunk12
我的代码:
d = {}
for line in output.split("\n"):
if ":" not in line:
continue
key, value = line.strip().split(":", 1)
d[key] = value
预期密钥及其值:
trunk_names: trunk01, trunk02, trunk03,trunk04,trunk05,trunk06, trunk07,trunk08,trunk09,trunk10, trunk11,trunk12
输出实际的键和值:
trunk_names: trunk01, trunk02, trunk03,trunk04,
答案 0 :(得分:1)
from collections import defaultdict
output = '''
goods: apples, oranges
trunk_names: trunk01, trunk02, trunk03,trunk04,
trunk05,trunk06, trunk07,trunk08,
trunk09,trunk10, trunk11,trunk12
'''
d = defaultdict(list)
current_key = None
for line in output.split('\n')[1:]:
if ":" in line:
current_key = line.split(':')[0].strip()
values = line.split(':')[1]
else:
values = line
d[current_key] += [
value.strip()
for value in values.split(',')
if value.strip()
]
print(d)
给出:
defaultdict(<type 'list'>, {'trunk_names': ['trunk01', 'trunk02', 'trunk03', 'trunk04', 'trunk05', 'trunk06', 'trunk07', 'trunk08', 'trunk09', 'trunk10', 'trunk11', 'trunk12'], 'goods': ['apples', 'oranges']})
答案 1 :(得分:0)
在你现有的代码中,你正在做的是跳过没有冒号的行,所以你丢弃了一半以上的数据。
您可以使用<key : list>
对词典将每个项目存储在由该键索引的列表中。
from collections import defaultdict
import pprint
output = '''goods: apples, oranges
trunk_names: trunk01, trunk02, trunk03,trunk04,
trunk05,trunk06, trunk07,trunk08,
trunk09,trunk10, trunk11,trunk12'''
d = defaultdict(list)
for line in output.split("\n"):
if ":" in line:
key, line = line.split(':')
items = line.strip().split(',')
d[key].extend([i.strip() for i in items if i]) # check to make sure we don't add '' items to the list
pprint.pprint(d)
在这里,我使用collections.defaultdict
,因此您无需在向其添加项目之前显式创建密钥条目。
运行此命令:
{'goods': ['apples', ' oranges'],
'trunk_names': ['trunk01',
'trunk02',
'trunk03',
'trunk04',
'trunk05',
'trunk06',
'trunk07',
'trunk08',
'trunk09',
'trunk10',
'trunk11',
'trunk12']}
答案 2 :(得分:0)
您的结构有多稳定,如果它非常稳定且数据质量很高,那么您可以通过测试line.endswith(',')
:
In []:
d = {}
f = iter(output.split('\n'))
for line in f:
key, line = map(str.strip, line.split(':', 1))
while line.endswith(','):
line += next(f)
d[key] = [i.strip() for i in line.split(',')]
pprint.pprint(d)
Out[]:
{'goods': ['apples', 'oranges'],
'trunk_names': ['trunk01',
'trunk02',
'trunk03',
'trunk04',
'trunk05',
'trunk06',
'trunk07',
'trunk08',
'trunk09',
'trunk10',
'trunk11',
'trunk12']}