如何将文本文件中的数据保存到python字典并选择指定数据

如何将文本文件中的数据保存到python字典并选择指定数据,python,dictionary,Python,Dictionary,txt文件中的数据示例: apple orange banana lemon pears 无字典过滤5个字母的单词代码: def numberofletters(n): file = open("words.txt","r") lines = file.readlines() file.close() for line in lines: if len(line) == 6: print(line)

txt文件中的数据示例:

apple
orange
banana
lemon
pears
无字典过滤5个字母的单词代码:

def numberofletters(n):
    file = open("words.txt","r")
    lines = file.readlines()
    file.close()
    for line in lines:
        if len(line) == 6:
            print(line)       

    return

print("===================================================================")
print("This program can use for identify and print out all words in 5 
letters from words.txt")
n = input("Please Press enter to start filtering words")
print("===================================================================")
numberofletters(n)
我的问题是如何创建一个字典,它的键是整数,并为有那么多字母的英语单词赋值,然后使用字典识别并打印出所有5个字母的单词


使用大量单词进行图像处理

您可以像这样创建for循环:

for line in lines:
    line_len = len(line)
    if line_len not in dicword.keys():
        dicword.update({line_len: [line]})
    else:
        dicword[line_len].append(line)

然后,您只需执行dicword[5]

即可获得它。您可以像这样创建for循环:

for line in lines:
    line_len = len(line)
    if line_len not in dicword.keys():
        dicword.update({line_len: [line]})
    else:
        dicword[line_len].append(line)

然后你只需做
dicword[5]

听起来像是一个
defaultdict
的工作

>>> from collections import defaultdict
>>> length2words = defaultdict(set)
>>> 
>>> with open('file.txt') as f:
...     for word in f: # one word per line
...         word = word.strip()
...         length2words[len(word)].add(word)
... 
>>> length2words[5]
set(['lemon', 'apple', 'pears'])

如果您关心重复项和插入顺序,请使用
defaultdict(list)
append
而不是
add

听起来像是
defaultdict
的作业

>>> from collections import defaultdict
>>> length2words = defaultdict(set)
>>> 
>>> with open('file.txt') as f:
...     for word in f: # one word per line
...         word = word.strip()
...         length2words[len(word)].add(word)
... 
>>> length2words[5]
set(['lemon', 'apple', 'pears'])

如果您关心重复项和插入顺序,请使用
defaultdict(list)
append
而不是
add

如果我理解,您需要编写筛选文档并将结果写入文件。为此,您可以使用DictWriter()编写CSV文件

DictWriter:创建一个像普通writer一样运行的对象,但将字典映射到输出行

顺便说一句,您将能够存储和组织您的文档

def numberofletters(n):
   file = open("words.txt","r")
   lines = file.readlines()
   file.close()
   dicword = {}
   writer = csv.DictWriter(filename, fieldnames=fieldnames)
   writer.writeheader()
   for line in lines:
       if len(line) == 6:
           writer.writerow({'param_label': line, [...]})
   return

我希望这对你有帮助

如果我理解,您需要编写筛选文档并将结果写入文件。为此,您可以使用DictWriter()编写CSV文件

DictWriter:创建一个像普通writer一样运行的对象,但将字典映射到输出行

顺便说一句,您将能够存储和组织您的文档

def numberofletters(n):
   file = open("words.txt","r")
   lines = file.readlines()
   file.close()
   dicword = {}
   writer = csv.DictWriter(filename, fieldnames=fieldnames)
   writer.writeheader()
   for line in lines:
       if len(line) == 6:
           writer.writerow({'param_label': line, [...]})
   return

我希望这对你有帮助

为什么在这里使用
update
,而不仅仅是
dicword[line\u len]=[line]
?除了我的习惯之外,没有什么特别的原因。你能告诉我代码是如何运行的(全部)吗?我试图输入到我的,但它不工作。感谢您用它替换函数中的for循环,您的返回应该是
return dicword[n]
为什么在这里使用
update
,而不仅仅是
dicword[line\u len]=[line]
?除了我的习惯之外,没有什么特别的原因。您能告诉我代码是如何运行的吗(全部)?我试图输入到我的,但它不工作。感谢您用它替换函数中的for循环,您的返回应该是
return dicword[n]
如何在没有任何导入函数的情况下执行?集合是标准python库的一部分,导入它不应该是问题。但是如果是这样的话,你可以使用一个数组的dict(因此wordcount[5]=[]),然后检查它是否存在于子数组的每个插入中,这样你就不会使程序崩溃)如何在没有任何导入函数的情况下进行操作?集合是标准python库的一部分,导入它不应该是一个问题。但是如果是,您可以使用一个数组的dict(因此wordcount[5]=[]),然后检查它是否存在于子数组的每个插入中,这样您就不会使程序崩溃)