[英]How to convert a file with words on different newlines into a dictionary based on values each word has?
I am trying to convert a file, where every word is on a different newline, into a dictionary where the keys are the word sizes and values are the lists of words.我正在尝试将每个单词都在不同换行符上的文件转换为字典,其中键是单词大小,值是单词列表。
The first part of my code has removed the newline characters from the text file, and now I am trying to organize the dictionary based on the values a word has.我的代码的第一部分已经从文本文件中删除了换行符,现在我正在尝试根据单词的值来组织字典。
with open(dictionary_file, 'r') as file:
wordlist = file.readlines()
print([k.rstrip('\n') for k in wordlist])
dictionary = {}
for line in file:
(key, val) = line.split()
dictionary[int(key)] = val
print(dictionary)
However, I keep getting the error that there aren't enough values to unpack, even though I'm sure I have already removed the newline characters from the original text file.但是,我一直收到没有足够的值来解包的错误,即使我确定我已经从原始文本文件中删除了换行符。 Another error I get is that it will only print out the words in a dictionary without the newlines, however, they aren't organized by value.
我得到的另一个错误是它只会打印出字典中没有换行符的单词,但是,它们不是按值组织的。 Any help would be appreciated, thanks: :)
任何帮助将不胜感激,谢谢::)
(key, val) = line.split()
^^^^^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)
I'm not sure why you're trying to use line.split()
.我不确定您为什么要尝试使用
line.split()
。 All you need is the length of the word, so you can use the len()
function. Also, you use collections.defaultdict
to make this code shorter.您只需要单词的长度,因此您可以使用
len()
function。此外,您还可以使用collections.defaultdict
来缩短此代码。 Like this:像这样:
import collections
words = collections.defaultdict(list)
with open('test.txt') as file:
for line in file:
word = line.strip()
words[len(word)].append(word)
try this尝试这个
with open(dictionary_file, 'r') as file:
dictionary = {}
for line in file:
val = line.strip().split()
dictionary[len(val)] = val
print(dictionary)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.