简体   繁体   English

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

[英]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.

相关问题 读取文本文件(用冒号分隔的单词)并获取元组列表 - read text file (colon-separated words) and get a list of tuples 读取文件并使用python中的字典获取前3个单词 - read a file and get the top 3 words using a dictionary in python 如何使用冒号分隔符从Python中的文本文件读取数据 - How to read data from text file in Python using a colon delimeter 在python中使用字典查找文本文件中的字典单词 - Looking for dictionary words in text file using dictionary in python 在带有冒号分隔数字的文本文件上使用numpy.fromregex - Using numpy.fromregex on a text file with colon separated digits 在 Python 中使用字典从文本文件中提取单词(查找和替换) - Extracting words from Text file (find and replace) using dictionary in Python 使用 Python 字典来计算单词的频率,不包括将从第二个文件中读取的一组“停用词” - using a Python dictionary to count the frequency of words, excluding a set of "stop words" that will be read from a second file 在Python中读取文本文件,用','和';'分隔 作为行终止者 - Read text file in Python, separated by ',' and with ';' as line terminator 如何将文本文件中的单词放入python字典中 - how to put the words in the text file into dictionary in python 如何使用 Python 读取文本文件中的特定单词 - How to read specific words in text file using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM