简体   繁体   English

我正在尝试在 TextWrangler 应用程序中使用 Python 读取、附加和排序文件中的所有单词。

[英]I am trying to read, append and sort all words in a file using Python in TextWrangler application.

Question : Open the file romeo.txt and read it line by line.问题:打开文件 romeo.txt 并逐行阅读。 For each line, split the line into a list of words using the split() method.对于每一行,使用 split() 方法将该行拆分为单词列表。 The program should build a list of words.该程序应该建立一个单词列表。 For each word on each line check to see if the word is already in the list and if not append it to the list.对于每行上的每个单词,检查该单词是否已经在列表中,如果没有,则将其附加到列表中。 When the program completes, sort and print the resulting words in alphabetical order.程序完成后,按字母顺序对结果单词进行排序和打印。

fname = raw_input('Enter the file name: ')
file = open(fname)
romeo = list()
for line in file:   
    words = line.split()    
    for current_word in words : 
        if current_word in romeo:   
            continue
        romeo.append(current_word)
print romeo.sort()

I am getting an output of "None".我得到“无”的输出。 I am not sure what I am doing wrong This link is where the romeo.txt file is found.我不确定我做错了什么此链接是找到 romeo.txt 文件的位置。

The reason why you are getting None is because romeo.sort() does the sorting in place, so it actually returns nothing.你得到None的原因是因为romeo.sort()进行了适当的排序,所以它实际上什么都不返回。 You have to actually just print romeo after you call romeo.sort() .实际上,您必须在调用romeo.sort()后才打印romeo

So, instead of printing romeo.sort() , just print:因此,不要打印romeo.sort() ,只需打印:

print(romeo)

Without continue :没有继续

fname = raw_input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
    word = line.split()
    for item in word:
      if item not in lst:
            lst.append(item)
            lst.sort()
print(lst)

fname = input("Enter file name: ") fname = input("请输入文件名:")

fh = open(fname) fh = 打开(fname)

lst = list() lst = 列表()

for line in fh:对于 fh 中的行:

x=line.split()

for word in x:

if word not in lst:
    lst.append(word)
    lst.sort()

print(lst)打印(lst)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在 python 中读取、附加和排序文本文件的所有单词? - How do i read, append and sort all the words of a text file in python? 尝试将带有数字的txt文件读入列表,然后使用Python进行排序 - Trying to read a txt file with numbers into a list and then sort using Python 我正在尝试通过python程序读取文件的内容 - I am trying to read the content of a file through a python program 我正在尝试读取 pdf 文件并将其显示在 python - I am trying to read a pdf file and display it in python 我正在尝试使用“a.sort([1])”用整数和浮点数对python中的列表进行排序,但它不起作用 - I am trying to sort a list in python with integers and a float using "a.sort([1])" and it is not working 我正在尝试读取日志文件。 它使用记事本作为txt文件打开。 但是,当我尝试使用python读取文件时,它无法打开 - I am trying to read a log file. It opens using notepad as txt file. However when I try using python to read the file it doesn't open 我正在尝试读取图像并将其转换为 python 中的双精度图像,我正在使用 cv2 库 - I am trying to read an image and convert it to double in python , I am using cv2 library 尝试使用Python计算文件中的单词 - Trying to count words in a file using Python 我正在尝试使用python从S3存储桶中删除文件,但无法删除该文件 - I am trying to delete file from S3 bucket using python but I am not able to delete the file 我正在尝试使用堆栈来反转 python 中的单词顺序 - I am trying to use a stack to reverse the order of words in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM