编写一个程序,该程序读取文本文件的内容并将单词的索引返回到“值”中

美浓

我正在从教科书上做练习,我坚持了三天,最后我决定在这里寻求帮助。

问题是:

编写一个读取文本文件内容的程序。该程序应创建一个字典,其中的键值对描述如下:

•键。键是在文件中找到的单个单词。

•价值观。每个值都是一个列表,其中包含在文件中找到单词(键)的行号。

例如:假设在第7、18、94和138行中找到“机器人”一词。字典中将包含一个元素,其中键为字符串“机器人”,而值是包含数字7的列表。 18、94和138。

构建字典后,程序应创建另一个文本文件,称为单词索引,列出字典的内容。单词索引文件应包含按字母顺序存储在字典中的单词的单词列表,以及单词在原始文件中出现的行号。

图9-1显示了原始文本文件(Kennedy.txt)及其索引文件(index.txt)的示例。

在此处输入图片说明

这是我到目前为止尝试过的代码,功能尚未完成,不确定下一步该怎么做:

def create_Kennedytxt():
    f = open('Kennedy.txt','w')
    f.write('We observe today not a victory\n')
    f.write('of party but a celebration\n')
    f.write('of freedom symbolizing an end\n')
    f.write('as well as a beginning\n')
    f.write('signifying renewal as well\n')
    f.write('as change\n')
    f.close()

create_Kennedytxt()
def split_words():
    f = open('Kennedy.txt','r')

    count = 0
    
    for x in f:
        y = x.strip()
        z = y.split(' ')    #get individual character to find its index
        count+=1            #get index for each line during for loop
   
                            
split_words()

谁能帮助我解答代码或给我一些提示?答案不应该是导入任何东西,而只能使用方法和函数来实现。我将不胜感激!

乔·费尔兹(Joe Ferndz)

这是一个三步过程:

  1. 逐行读取文件并将每一行拆分为单词
  2. Identify all unique words in each line (use set to do this)
  3. For each word, check if word exists in the dictionary.
    • If it exists in the dictionary, then add the line number (line starts with 0, so you may need to add +1) to add 1 to it)
    • If it does NOT exist in the dictionary, create a new key entry for the word and include the line number.

The dictionary will be a keys with lists.

To do this, you can create a program like this:

keys_in_file = {}
with open ('Kennedy.txt', 'r') as f:
    for i,line in enumerate(f):
        words = line.strip().split()
        for word in set(words):
            keys_in_file.setdefault(word, []).append(i+1) 

print (keys_in_file)

The output of the file you provided (Kennedy.txt) is:

{'today': [1], 'victory': [1], 'observe': [1], 'a': [1, 2, 4], 'We': [1], 'not': [1], 'celebration': [2], 'of': [2, 3], 'party': [2], 'but': [2], 'freedom': [3], 'an': [3], 'symbolizing': [3], 'end': [3], 'as': [4, 5, 6], 'well': [4, 5], 'beginning': [4], 'renewal': [5], 'signifying': [5], 'change': [6]}

If you want to ensure that all words (We, WE, we) get counted as same word, you need to convert words to lowercase.

words = line.lower().strip().split()

If you want the values to be printed in the format of index.txt, then you add the following to the code:

for k in sorted(keys_in_file):
    print (k+':', *keys_in_file[k])

输出将如下所示:注意:我转换We为小写字母,以便稍后以字母顺序显示

a: 1 2 4
an: 3
as: 4 5 6
beginning: 4
but: 2
celebration: 2
change: 6
end: 3
freedom: 3
not: 1
observe: 1
of: 2 3
party: 2
renewal: 5
signifying: 5
symbolizing: 3
today: 1
victory: 1
we: 1
well: 4 5

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 C# 中编写一个读取文本文件并输出总行数的程序?

如何编写逐行读取文本文件并删除所有“the”字并将其写入另一个文件的程序

文本处理-两个文本文件:从一个文件中读取程序行,并将其追加到另一个文本文件中的字符串之后

如何读取文本文件中的第一个单词并将其显示在richTextBox中

如何编写一个从二进制文件读取并写入文本文件的子程序?

编写一个Bash shell脚本以读取mongodb日志文件并将其内容写入另一个文本文件

读取文本文件一行中的第一个单词时出现索引错误

我的单词提示程序仅记录文本文件中的最后一个单词。我哪里做错了?

如何编写一个for循环以在Matlab中读取1000个文本文件?

如何从一个文本文件读取然后在一个控制台应用程序中写入另一个文件?

如何从文本文件中找到一个单词,然后在C#上读取该单词之后的下一个单词

读取文本文件后,Python程序打印一个空白行

如何创建一个程序,可以从给定的任何文本文件读取?

使用Automator读取文本文件并将内容复制到另一个文件夹

编写一个程序,该程序将计算c中一个文件中的单词总数

使用filepicker选择一个文本文件并将该文本文件的内容加载到Xamarin Android中的“编辑文本”中

编写一个程序,接受用户的输入并将其与文件中的单词进行比较

从文本文件中读取特定单词,然后保存下一个单词

该程序无法读取文本文件?

如何只读取文本文件中的第一个值

读取目录中的所有文本文件并将数据写入一个json文件中

编写一个Bash脚本,将每个逗号分隔的单词从文本文件中拉出

如何从终端读取文本文件并将输出保存到Java中的另一个文件?

如何获取多个动态文本文件的内容并将其存储在一个文本文件和mysql-Python 2.7中?

Java IO:使用扫描仪和printWriter复制文本文件的内容并将其放在另一个文本文件中

使用Java格式化文本文件内容后,如何读取文本文件内容并写入另一个文本文件?

如何复制文本文件的内容并将其粘贴到从某行开始的另一个文件中?

如何制作一个返回JavaScript中本地文本文件值的函数?

设计一个装饰器在文本文件中写入函数返回值