[英]read text file (colon separated words) and get a dictionary using python
hay i been trying to read from a text file then convert it into a dictionary.我一直试图从文本文件中读取,然后将其转换为字典。 this what i have in the text file这是我在文本文件中的内容
("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")
the out put needs to be输出需要是
{
("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")
}
this how i so far read from the file,这是我到目前为止从文件中读取的方式,
many thanks in advance提前谢谢了
I may offer我可以提供
data = open(...).readlines()
dct = {line.split(":")[0]:line.split(":")[1] for line in data}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.