简体   繁体   English

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

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

Task: given a txt file with adjective \\t synonym, synonym, synonym, etc. in a line, several lines are given.任务:给定一个txt文件,一行中含有形容词\\t同义词、同义词、同义词等,给出几行。 I need to create a dictionary, where adjective will be a key and synonyms - a value.我需要创建一个字典,其中形容词将是一个键和同义词 - 一个值。 My code:我的代码:

#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()

The output shows me the empty brackets... Could someone help to fix?输出显示了空括号......有人可以帮忙解决吗?

Instead of regular expressions, use split() to split strings using delimiters.代替正则表达式,使用split()使用分隔符分割字符串。 First split it using \\t to separate the adjective from the synonyms, then split the synonyms into a list using , .首先使用\\t将其拆分以将形容词与同义词分开,然后使用,将同义词拆分为列表。

Then you need to add a new key in the dictionary, not replace the entire dictionary.然后你需要在字典中添加一个新的键,而不是替换整个字典。

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