python - 从文件中打印包含字母范围内的行,不包括所需的限制

标签 python for-loop readlines

我在这个程序中遇到的问题是,即使我使用 >= <= 运算符,它也不包含边界。另外,由于某种原因,输出的单词均由换行符分隔,而不是一个接一个地打印。

例如,如果所选的 .txt 文件包含:

Aladdin
Batman
Dinosaurs
Edgar
Fruitloop
Mongoose

选择的上限和下限是:

Batman
Fruitloop

程序打印:

Batman

Dinosaurs

Edgar


这是我正在处理的内容。非常感谢任何帮助!

import os

user_file = input() #reads name of user chosen .txt file containing alphabetized one word per line lists
lo_limit = input() #reads a user chosen word as the inclusive lower alphabetical limit
up_limit = input() #reads a user chosen word as the inclusive upper alphabetical limit

file_handle = open(user_file) #opens user chosen file

lines = file_handle.readlines() #creates by-line string of file contents


#if user chosen file contains words equal to or between bounds, prints words
for ln in lines:
    if ln >= lo_limit \
        and ln <= up_limit:
        print(ln)

最佳答案

发生这种情况是因为当您执行f.readlines()时,这将返回如下列表:

f.readlines()
>>>['Aladdin\n', 'Batman\n', 'Dinosaurs\n', 'Edgar\n', 'Fruitloop\n', 'Mongoose']

当您输入 up_limit=Edgar 时,您会将列表中的每个 f.readlines() 与单词 Edgar 进行比较,如下所示这个:

'Aladdin\n'>=lo_limit and 'Aladdin\n'<='Edgar'
>>>True
'Batman\n'>=lo_limit and ''Batman\n''<='Edgar'
>>>True
....
....
....

当成为 'Edgar\n' 的迭代时,您可以检查:

'Edgar'>='Edgar\n'
Out[6]: False

这就是为什么没有打印“Edgar”的原因。 您可以尝试:

import os

user_file = input() #reads name of user chosen .txt file containing alphabetized one word per line lists
lo_limit = input() #reads a user chosen word as the inclusive lower alphabetical limit
up_limit = input() #reads a user chosen word as the inclusive upper alphabetical limit

with open(str(user_file)) as file_handle:#opens user chosen file
    lines = file_handle.readlines()
    #if user chosen file contains words equal to or between bounds, prints words
    for ln in lines:
        if (ln > lo_limit) or (ln == lo_limit) or (ln < up_limit):
            print(ln)
            if (ln == up_limit+'\n'):
                break

或者您可以按索引选择:

user_file = input() #reads name of user chosen .txt file containing alphabetized one word per line lists
lo_limit = str(input()) #reads a user chosen word as the inclusive lower alphabetical limit
up_limit = str(input()) #reads a user chosen word as the inclusive upper alphabetical limit

with open(str(user_file)) as file_handle:#opens user chosen file
    lines = file_handle.readlines() #creates by-line string of file contents    
    linesselected=lines[lines.index(lo_limit+'\n'):(lines.index(up_limit+'\n')+1)]
    for i in linesselected:
        print(i.replace('\n',''))

关于python - 从文件中打印包含字母范围内的行,不包括所需的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62494052/

相关文章:

r - 使用 for 循环执行多个回归

python - 如何创建和写入 textiowrapper 和 readlines

带有新版本 R 的 readLines 函数

python gensim 从 doc2vec taggedlinedocument 中检索原始句子

python - 如何在 Python 中编码/解码这个 BeautifulSoup 字符串,以便输出非标准的拉丁字符?

java - 什么时候调用静态循环?

python - 循环似乎不遵循顺序

python - 如何在 Python 中读取 txt 文件的特定部分?

python - ipython 中有没有办法始终在启动时运行导入?像 'from __future__ import division' 吗?

python - 用于 patchcollection 的带有颜色的图例