无法识别 Python 中的循环变量
Not Recognizing Loop Variable in Python
在 Python 的 .txt 文件中遇到某个短语后,我试图删除 38 行文本,同时仍打印其余文本。
我目前的代码是
with open('text_file.txt','r') as f:
lines = f.readlines()
for line in lines:
if "certain_phrase" in line:
for num in range(38):
del line
else:
print(line,end='')
但是,我不断收到以下错误:
Traceback (most recent call last):
File "C:\<location of file>\python_program.py", line 6, in <module>
del line
NameError: name 'line' is not defined
有人对我将其放入下面的 for 循环后无法识别 "line" 的原因提出任何建议或线索吗?另外,有没有更好的方法来执行这种程序?
del line
实际上删除了变量 line
,这意味着当您第二次尝试这样做时,它不起作用,因为 line
不再被定义。您可以遍历索引以找到该行,中断,然后删除接下来的 38 行:
with open('text_file.txt','r') as f:
lines = f.readlines()
for i in range(len(lines)):
if "certain_phrase" in lines[i]:
break
else:
print(line,end='')
for num in range(38):
del lines[i]
您需要从列表中删除,您不能 del
该行,最简单的方法是写入临时文件并在之后复制如果您想要修改文件,如果您只是想打印忽略第 38 行用打印替换写入:
with open('in.txt','r') as f,open('temp.txt','w') as temp:
for line in f:
if "phrase" in line:
for i in range(38):
next(f) # skip 38 lines
else:
temp.write(line)
然后使用shutil移动文件:
import shutil
shutil.move("temp.txt","in.txt")
您也可以使用 NamedTemporaryFile:
from tempfile import NamedTemporaryFile
with open('file.txt','r') as f, NamedTemporaryFile(dir=".",delete=False) as temp:
for line in f:
if "phrase" in line:
for i in range(38):
next(f)
else:
temp.write(line)
import shutil
shutil.move(temp.name,"file.txt")
我看到的唯一潜在问题是该短语是否在 38 行被忽略的行之一中,您还应该从那里删除接下来的 38 行。
To ignore until a second phrase, keep looping in the inner loop until you find the second phrase then break:
with open('in.txt','r') as f, NamedTemporaryFile(dir=".", delete=False) as temp:
for line in f:
if "phrase" in line:
for _line in f:
if "phrase2" in _line:
break
else:
temp.write(line)
with open('temp.txt','r') as fin:
for line in fin:
print(line,end="") #you want to print the phrase, right?
if "certain_phrase" in line:
for _ in range(38):
next(line)
与其尝试从文件中删除行,不如在旧文件的基础上编写一个新文件。以下使用 __next__()
跳过生成器产生的 line
s。
with open('text_file.txt','r') as f, open('text_file_mod.txt', 'w') as w:
for line in f:
w.write(line)
if "certain_phrase" in line:
for num in range(38): # skip 38 lines
next(f)
如果您是从交互式解释器执行此操作,则可以通过将 next(f)
和 w.write(line)
的结果保存到变量来防止它吐出返回值。
在 Python 的 .txt 文件中遇到某个短语后,我试图删除 38 行文本,同时仍打印其余文本。
我目前的代码是
with open('text_file.txt','r') as f:
lines = f.readlines()
for line in lines:
if "certain_phrase" in line:
for num in range(38):
del line
else:
print(line,end='')
但是,我不断收到以下错误:
Traceback (most recent call last):
File "C:\<location of file>\python_program.py", line 6, in <module>
del line
NameError: name 'line' is not defined
有人对我将其放入下面的 for 循环后无法识别 "line" 的原因提出任何建议或线索吗?另外,有没有更好的方法来执行这种程序?
del line
实际上删除了变量 line
,这意味着当您第二次尝试这样做时,它不起作用,因为 line
不再被定义。您可以遍历索引以找到该行,中断,然后删除接下来的 38 行:
with open('text_file.txt','r') as f:
lines = f.readlines()
for i in range(len(lines)):
if "certain_phrase" in lines[i]:
break
else:
print(line,end='')
for num in range(38):
del lines[i]
您需要从列表中删除,您不能 del
该行,最简单的方法是写入临时文件并在之后复制如果您想要修改文件,如果您只是想打印忽略第 38 行用打印替换写入:
with open('in.txt','r') as f,open('temp.txt','w') as temp:
for line in f:
if "phrase" in line:
for i in range(38):
next(f) # skip 38 lines
else:
temp.write(line)
然后使用shutil移动文件:
import shutil
shutil.move("temp.txt","in.txt")
您也可以使用 NamedTemporaryFile:
from tempfile import NamedTemporaryFile
with open('file.txt','r') as f, NamedTemporaryFile(dir=".",delete=False) as temp:
for line in f:
if "phrase" in line:
for i in range(38):
next(f)
else:
temp.write(line)
import shutil
shutil.move(temp.name,"file.txt")
我看到的唯一潜在问题是该短语是否在 38 行被忽略的行之一中,您还应该从那里删除接下来的 38 行。
To ignore until a second phrase, keep looping in the inner loop until you find the second phrase then break:
with open('in.txt','r') as f, NamedTemporaryFile(dir=".", delete=False) as temp:
for line in f:
if "phrase" in line:
for _line in f:
if "phrase2" in _line:
break
else:
temp.write(line)
with open('temp.txt','r') as fin:
for line in fin:
print(line,end="") #you want to print the phrase, right?
if "certain_phrase" in line:
for _ in range(38):
next(line)
与其尝试从文件中删除行,不如在旧文件的基础上编写一个新文件。以下使用 __next__()
跳过生成器产生的 line
s。
with open('text_file.txt','r') as f, open('text_file_mod.txt', 'w') as w:
for line in f:
w.write(line)
if "certain_phrase" in line:
for num in range(38): # skip 38 lines
next(f)
如果您是从交互式解释器执行此操作,则可以通过将 next(f)
和 w.write(line)
的结果保存到变量来防止它吐出返回值。