读取文本文件(冒号分隔的单词)并使用python获取字典

时间:2016-05-01 17:16:53

标签: python dictionary

hay我一直试图从文本文件中读取然后将其转换为字典。 这就是我在文本文件中的内容

("Tom","1"):("Tom", "x", "R")
("Tom","_"):("Matt", "_", "L")
("Matt","1"):("Matt", "1", "L")
("Matt","x"):("Dan", "1", "R")
("Matt","_"):("May", "_", "N")
("Matt","1"):("Dan", "1", "R")
("May","_"):("May", "1", "L")

输出需要

 {
    ("Tom","1"):("Tom", "x", "R"),
    ("Tom","_"):("Matt", "_", "L"),
    ("Matt","1"):("Matt", "1", "L"),
    ("Matt","x"):("Dan", "1", "R"),
    ("Matt","_"):("May", "_", "N"),
    ("Matt","1"):("Dan", "1", "R"),
    ("May","_"):("May", "1", "L")
}

这是我到目前为止从文件中读取的,

非常感谢提前

1 个答案:

答案 0 :(得分:0)

我可以提供

data = open(...).readlines()
dct = {line.split(":")[0]:line.split(":")[1] for line in data}