简体   繁体   English

将文本文件转换为字典

[英]Converting Text file to Dictionary

I have a text file called dict1.txt that contains two lines and I'd like to make the first string to give the keys and the second to give the values. 我有一个名为dict1.txt的文本文件,其中包含两行,我想使第一个字符串给出键,第二个字符串给出值。 The text file is 文本文件是

abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ

gikaclmnqrpoxzybdefhjstuvw gikaclmnqrpoxzybdefhjstuvw

and the code I've been toying with is: 我一直在玩的代码是:

def read_cipherdef(fn):
d = {}
fn = open('dict1.txt', 'r')
for line in fn:
    key, val = line.split()
    d[(key)] = val

but in this case line.split() doesn't actually split the first line by character and it also gives a ValueError: too many values to unpack (expected 2). 但在这种情况下,line.split()实际上并没有按字符分割第一行,而且它还提供了一个ValueError:解压缩的值太多(预期为2)。

Also I'm aware list(map(''.join, zip(*[iter('dict1.txt')]*1))) would split them up individually, but using that in conjunction with the existing code gives me another ValueError. 另外我知道list(map(''.join, zip(*[iter('dict1.txt')]*1)))会将它们分开,但是将它与现有代码结合使用会给我另一个ValueError异常。 So my question would be: how would I use all of this info here together to achieve my desired dictionary? 所以我的问题是:我如何在这里一起使用所有这些信息来实现我想要的字典?

Thanks. 谢谢。

Here's one way you can achieve that. 这是您实现这一目标的一种方式。 Essentially, read the two lines, zip them and use them in a dict-comprehension. 基本上,读取两行,压缩它们并在字典理解中使用它们。

>>> with open("/tmp/t") as f:
...     line1 = f.readline().strip()
...     line2 = f.readline().strip()
...     print({x:y for x, y in zip(line1, line2)})
... 
{'a': 'g', 'b': 'i', 'c': 'k', 'd': 'a', 'e': 'c', 'f': 'l', 'g': 'm', 'h': 'n', 'i': 'q', 'j': 'r', 'k': 'p', 'l': 'o', 'm': 'x', 'n': 'z', 'o': 'y', 'p': 'b', 'q': 'd', 'r': 'e', 's': 'f', 't': 'h', 'u': 'j', 'v': 's', 'w': 't', 'x': 'u', 'y': 'v', 'z': 'w'}

.. Or if you want a one-liner (this assumes there are only 2 lines in the file): ..或者如果你想要一个单行(假设文件中只有2行):

>>> with open("/tmp/t") as f:
...     print({x: y for x, y in zip(*[list(x.strip()) for x in f])})
... 
{'a': 'g', 'b': 'i', 'c': 'k', 'd': 'a', 'e': 'c', 'f': 'l', 'g': 'm', 'h': 'n', 'i': 'q', 'j': 'r', 'k': 'p', 'l': 'o', 'm': 'x', 'n': 'z', 'o': 'y', 'p': 'b', 'q': 'd', 'r': 'e', 's': 'f', 't': 'h', 'u': 'j', 'v': 's', 'w': 't', 'x': 'u', 'y': 'v', 'z': 'w'}

How about this? 这个怎么样?

with open('dict1.txt') as f:
    lines=[line.strip('\n') for line in f.readlines()]
    d=dict(zip(lines[0],lines[1]))

from pprint import pprint
pprint(d)

Result: 结果:

{'a': 'g',
 'b': 'i',
 'c': 'k',
 'd': 'a',
 'e': 'c',
 'f': 'l',
 'g': 'm',
 'h': 'n',
 'i': 'q',
 'j': 'r',
 'k': 'p',
 'l': 'o',
 'm': 'x',
 'n': 'z',
 'o': 'y',
 'p': 'b',
 'q': 'd',
 'r': 'e',
 's': 'f',
 't': 'h',
 'u': 'j',
 'v': 's',
 'w': 't',
 'x': 'u',
 'y': 'v',
 'z': 'w'}

You can, also, do it like this way: 你也可以这样做:

data = (k.rstrip() for k in open("dict1.txt", 'r'))
a = {k:v for k,v in zip(*data)}
print(a)

Or maybe in one line: 或者可能在一行中:

a = {k:v for k,v in zip(*(k.rstrip() for k in open("dict1.txt", 'r')))}
print(a)

Output: 输出:

{'a': 'g', 'y': 'v', 'k': 'p', 'j': 'r', 'f': 'l', 'g': 'm', 'o': 'y', 'r': 'e', 's': 'f', 'b': 'i', 'e': 'c', 'u': 'j', 't': 'h', 'z': 'w', 'l': 'o', 'c': 'k', 'h': 'n', 'i': 'q', 'n': 'z', 'd': 'a', 'w': 't', 'x': 'u', 'v': 's', 'm': 'x', 'p': 'b', 'q': 'd'}

Instead of the comprehension you can use dict directly: 您可以直接使用dict而不是理解:

dict(zip(*open('dict1.txt', 'rt').read().strip().split()))

Output: 输出:

{'a': 'g', 'b': 'i', 'c': 'k', 'd': 'a', 'e': 'c', 'f': 'l', 'g': 'm', 'h': 'n', 'i': 'q', 'j': 'r', 'k': 'p', 'l': 'o', 'm': 'x', 'n': 'z', 'o': 'y', 'p': 'b', 'q': 'd', 'r': 'e', 's': 'f', 't': 'h', 'u': 'j', 'v': 's', 'w': 't', 'x': 'u', 'y': 'v', 'z': 'w'}

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

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