任务:给定一个包含形容词\t同义词、同义词、异名等的txt文件。在一行中,会给出几行。我需要创建一个字典,其中形容词将是一个关键字,同义词-一个值。我的代码:
#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)