繁体   English   中英

如何从给定的txt文件制作字典?

[英]How to make a dictionary from a given txt file?

任务:给定一个txt文件,一行中含有形容词\\t同义词、同义词、同义词等,给出几行。 我需要创建一个字典,其中形容词将是一个键和同义词 - 一个值。 我的代码:

#necessary for command line + regex
import sys 
import re

#open file for reading
filename = sys.argv[1]
infile = open(filename, "r")

#a
#create a dictionary, where an adjective in a line is a key
#and synonyms are the value

dictionary = {}
#for each line in infile
for line in infile:
    
    #creating a list with keys, a key is everything before the tab
    adjectives = re.findall(r"w+\t$", line)
    print(adjectives)
    
    #creating a list of values, a value is everything after the tab
    synonyms = re.findall(r"^\tw+\n$", line)
    print(synonyms)
    
    #combining both lists into a dictionary, where adj are keys, synonyms - values
    dictionary = dict(zip(adjectives, synonyms))
    print(dictionary)

#close the file
infile.close()

输出显示了空括号......有人可以帮忙解决吗?

代替正则表达式,使用split()使用分隔符分割字符串。 首先使用\\t将其拆分以将形容词与同义词分开,然后使用,将同义词拆分为列表。

然后你需要在字典中添加一个新的键,而不是替换整个字典。

for line in infile:
    line = line.strip() # remove newline
    adjective, synonyms = line.split("\t")
    synonyms = synonyms.split(",")
    dictionary[adjective] = synonyms

print(dictionary)

暂无
暂无

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

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