简体   繁体   English

如何根据每个单词的值将包含不同换行符的单词的文件转换为字典?

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

相关问题 如何根据字典将数字转换为单词? - How to convert a number into words based on a dictionary? 为文件中的每个单词创建一个字典,并计算其后的单词的频率 - Creating a dictionary for each word in a file and counting the frequency of words that follow it 如何根据 Pandas 中具有三种不同条件的另一列中的单词检查一列是否包含单词? - How to check if a column has a word based on words from another column with three different conditions in Pandas? 如何在php或python中创建每个单词出现频率的单词词典 - How to create a dictionary of words with frequency of each word in php or python 如何将字符串转换为字典,并计算每个单词的数量 - How to convert string to dictionary, and count the number of each word 如何从单词文件的几行中拆分每个单词? (Python) - How to split each words from several lines of a word file? (python) 如何转换。 按关键字转换为不同格式的示例字典? - How to convert. a sample dictionary to a different format by key word? 创建字符串中的单词字典,其值是该单词之后的单词 - creating a dictionary of words in string whose values are words following that word 如何将字典值转换为csv文件? - How to convert dictionary values into a csv file? 如何将 csv 文件转换为 Python 字典并根据特定条件求和/操作值? - How to convert csv file into Python dictionary and sum/operate values based on certain conditions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM