[英]Creating a Dictionary from txt
I have a couple of lines inside a text that i am looking to turn the first word to a key (space is between each) with a function, and the rest to follow as values. 我在文本中有几行,我希望将第一个单词转换为带有功能的键(每个键之间有空格),其余作为值。 This is what the text contains:
这是文本所包含的内容:
FFFB10 11290 Charlie
1A9345 37659 Delta
221002 93323 Omega
The idea is to turn the first word into a key, but also arrange it (row underneath a row) visualy, so the first word(FFFB10) is the key, and the rest are values, meaning: 这个想法是将第一个单词变成一个键,但还要视觉上排列它(行下面),因此第一个单词(FFFB10)是键,其余的是值,表示:
Entered: FFFB10
Location: 11290
Name: Charlie
I tried with this as a beginning: 我以此为起点进行了尝试:
def code(codeenter, file):
for line in file.splitlines():
if name in line:
parts = line.split(' ')
But i dont know how to continue (i erased most of the code), any suggestions? 但是我不知道如何继续(我删除了大部分代码),有什么建议吗?
Assuming you managed to extract a list of lines without the newline character at the end. 假设您设法提取出行尾没有换行符的列表。
def MakeDict(lines):
return {key: (location, name) for key, location, name in (line.split() for line in lines)}
This is an ordinary dictionary comprehension with a generator expression . 这是带有生成器表达式的普通字典理解 。 The former is all the stuff in brackets and the latter is inside the last pair of brackets.
前者放在方括号中,而后者在最后一对方括号内。
line.split
splits a line with whitespace being the delimiter. line.split
用空格作为分隔符分割一行。
Example run: 示例运行:
>>> data = '''FFFB10 11290 Charlie
... 1A9345 37659 Delta
... 221002 93323 Omega'''
>>> lines = data.split('\n')
>>> lines
['FFFB10 11290 Charlie', '1A9345 37659 Delta', '221002 93323 Omega']
>>> def MakeDict(lines):
... return {key: (location, name) for key, location, name in (line.split() for line in lines)}
...
>>>
>>> MakeDict(lines)
{'FFFB10': ('11290', 'Charlie'), '1A9345': ('37659', 'Delta'), '221002': ('93323', 'Omega')}
How to format the output: 如何格式化输出:
for key, values in MakeDict(lines).items():
print("Key: {}\nLocation: {}\nName: {}".format(key, *values))
See ForceBru's answer on how to construct the dictionary. 有关如何构造字典的信息,请参见ForceBru的答案。 Here's the printing part:
这是打印部分:
for k, (v1, v2) in your_dict.items():
print("Entered: {}\nLocation: {}\nName: {}\n".format(k, v1, v2))
You can try this: 您可以尝试以下方法:
f = [i.strip('\n').split() for i in open('filename.txt')]
final_dict = {i[0]:i[1:] for i in f}
Assuming the data is structured like this: 假设数据的结构如下:
FFFB10 11290 Charlie
1A9345 37659 Delta
221002 93323 Omega
Your output will be: 您的输出将是:
{'FFFB10': ['11290', 'Charlie'], '221002': ['93323', 'Omega'], '1A9345': ['37659', 'Delta']}
You may want to consider using namedtuple
. 您可能要考虑使用
namedtuple
。
from collections import namedtuple
code = {}
Code = namedtuple('Code', 'Entered Location Name')
filename = '/Users/ca_mini/Downloads/junk.txt'
with open(filename, 'r') as f:
for row in f:
row = row.split()
code[row[0]] = Code(*row)
>>> code
{'1A9345': Code(Entered='1A9345', Location='37659', Name='Delta'),
'221002': Code(Entered='221002', Location='93323', Name='Omega'),
'FFFB10': Code(Entered='FFFB10', Location='11290', Name='Charlie')}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.