在不包括所需限制的包含字母范围内的文件中打印行
printing lines from a file within an inclusive alphabetical range not including desired limits
我在使用该程序时遇到的问题是,即使我使用 >= <= 运算符,它也不包括边界。同样出于某种原因,输出的每个单词都由换行符分隔,而不是一个接一个地打印。
例如,如果所选的 .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)
好的,从打开文件的方式开始,使用这样的上下文管理器更容易打开它,然后它会为您处理 opening/closing。
with open('input.txt') as f:
lines = f.readlines()
关于为什么您的代码不起作用,您必须考虑 file_handle.readlines() 正在执行和存储的内容。我相信您的印象是 lines
包含:
['Aladdin', 'Batman', 'Dinosaurs', 'Edgar', 'Fruitloop', 'Mongoose']
实际上它包含:
['Aladdin\n', 'Batman\n', 'Dinosaurs\n', 'Edgar\n', 'Fruitloop\n', 'Mongoose']
您可以像这样使用列表理解来剥离换行转义字符:
lines = [l.replace('\n', '') for l in lines]
那么你的逻辑应该没问题。总之,尝试这样的事情:
with open('input.txt') as f:
lines = f.readlines()
lines = [l.replace('\n', '') for l in lines]
print(lines)
lo_limit = 'Batman'
up_limit = 'Fruitloop'
for ln in lines:
if ln >= lo_limit and ln <= up_limit:
print(ln)
给出输出:
['Aladdin', 'Batman', 'Dinosaurs', 'Edgar', 'Fruitloop', 'Mongoose']
Batman
Dinosaurs
Edgar
Fruitloop
这是不是如何select一系列行。这个案例是有效的,因为输入是 ascending 顺序。给它一个随机输入,你不会得到你期望的。
lines = """Aladdin
Batman
Dinosaurs
Edgar
Axe # input to break the ascending order
Fruitloop
Mongoose"""
lines = lines.split("\n")
for i in range(len(lines)):
if "Batman" == lines[i]:
for j in range(i, len(lines)):
print(lines[j])
if "Fruitloop" == lines[j]:
break
要获得一定范围的行,您首先需要在行上循环,找到起始行,然后从该行开始循环,直到找到结束行。
另外:始终使用 with 子句打开文件:
with open(file, "r") as file:
for line in file:
# read line by line here
发生这种情况是因为当您执行 f.readlines()
这将 return 一个像这样的列表:
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
或者您可以 select 按索引:
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',''))
您需要将“>=”和“<=”替换为“>”和“<”。同时从每一行中删除“\n”。
要将结果留在同一行,您需要使用打印函数的结束属性。
保持这样:
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:
ln = ln.replace('\n', '')
if ln > lo_limit \
and ln < up_limit:
print(ln, end=' ')
输出:
$ python file.py
arquivo.txt
Aladdin
Mongoose
Batman Dinosaurs Edgar Fruitloop
我在使用该程序时遇到的问题是,即使我使用 >= <= 运算符,它也不包括边界。同样出于某种原因,输出的每个单词都由换行符分隔,而不是一个接一个地打印。
例如,如果所选的 .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)
好的,从打开文件的方式开始,使用这样的上下文管理器更容易打开它,然后它会为您处理 opening/closing。
with open('input.txt') as f:
lines = f.readlines()
关于为什么您的代码不起作用,您必须考虑 file_handle.readlines() 正在执行和存储的内容。我相信您的印象是 lines
包含:
['Aladdin', 'Batman', 'Dinosaurs', 'Edgar', 'Fruitloop', 'Mongoose']
实际上它包含:
['Aladdin\n', 'Batman\n', 'Dinosaurs\n', 'Edgar\n', 'Fruitloop\n', 'Mongoose']
您可以像这样使用列表理解来剥离换行转义字符:
lines = [l.replace('\n', '') for l in lines]
那么你的逻辑应该没问题。总之,尝试这样的事情:
with open('input.txt') as f:
lines = f.readlines()
lines = [l.replace('\n', '') for l in lines]
print(lines)
lo_limit = 'Batman'
up_limit = 'Fruitloop'
for ln in lines:
if ln >= lo_limit and ln <= up_limit:
print(ln)
给出输出:
['Aladdin', 'Batman', 'Dinosaurs', 'Edgar', 'Fruitloop', 'Mongoose']
Batman
Dinosaurs
Edgar
Fruitloop
这是不是如何select一系列行。这个案例是有效的,因为输入是 ascending 顺序。给它一个随机输入,你不会得到你期望的。
lines = """Aladdin
Batman
Dinosaurs
Edgar
Axe # input to break the ascending order
Fruitloop
Mongoose"""
lines = lines.split("\n")
for i in range(len(lines)):
if "Batman" == lines[i]:
for j in range(i, len(lines)):
print(lines[j])
if "Fruitloop" == lines[j]:
break
要获得一定范围的行,您首先需要在行上循环,找到起始行,然后从该行开始循环,直到找到结束行。
另外:始终使用 with 子句打开文件:
with open(file, "r") as file:
for line in file:
# read line by line here
发生这种情况是因为当您执行 f.readlines()
这将 return 一个像这样的列表:
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
或者您可以 select 按索引:
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',''))
您需要将“>=”和“<=”替换为“>”和“<”。同时从每一行中删除“\n”。
要将结果留在同一行,您需要使用打印函数的结束属性。
保持这样:
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:
ln = ln.replace('\n', '')
if ln > lo_limit \
and ln < up_limit:
print(ln, end=' ')
输出:
$ python file.py
arquivo.txt
Aladdin
Mongoose
Batman Dinosaurs Edgar Fruitloop