无法使用上下文管理器遍历文件 python3
Unable to loop over a file using context manager python3
instances = ['XYZ']
curr_date = time.strftime("%Y%m%d")
for inst_name in instances:
with open(f'results_{inst_name}_master.csv') as f:
pkt_name = f.readline().split(';')[1]
print(f"package name is: {pkt_name}")
run_dt = f.readline().split(';')[1]
print(f"run date: {run_dt}")
time_check = f.readlines()[1].split(';')[-5].split(':')[0]
print(f"time is : {time_check}")
if ((run_dt == curr_date) or (run_dt < curr_date and time_check in range(20,24))):
for index, l in enumerate(f, 1):
if 'remoteInstallation' in l:
print(l)
示例文件:
pktName;sample-quality.zip;
RunDate;20220116;
sample:path_to_test\test;20220116;01:29:13;20220114;01:33:36;0:04:22;
;sample:path\to\remoteInstallation;45.99;../logs_path/index.html;OK;
;sample:path\to\remoteInstallation;42.87;../logs__other_path/index.html;1
我得到了除 print(l)
之外的所有结果,也就是说,它无法遍历文件,尽管文件中存在字符串:'remoteInstallation'。
请注意,实际上,文件在每行的末尾包含换行符(\n)。
我做错了什么?
提前致谢!
您似乎在 time_check 行上使用 readlines()
而不是 .readline()
。
import time
instances = ['XYZ']
curr_date = time.strftime("%Y%m%d")
for inst_name in instances:
with open(f'results_{inst_name}_master.csv') as f:
pkt_name = f.readline().split(';')[1]
print(f"package name is: {pkt_name}")
run_dt = f.readline().split(';')[1]
这行似乎不正确。它调用 readlines
并引用第一个索引处的元素。我删除了 s
和索引引用。
time_check = f.readline().split(';')[-5].split(':')[0]
print(f"time is : {time_check}")
if ((run_dt == curr_date) or (run_dt < curr_date and time_check in range(20,24))):
for index, l in enumerate(f, 1):
if 'remoteInstallation' in l:
print(l)
幕后发生了什么?
对于文件对象(在本例中 f
),有一个内部指针跟踪脚本当前在文件中查找的位置。这个指针是通用的,被多个函数和 for 循环使用。
每次调用 readline()
都会从文件中读取一行并将指针移动到下一行。
对 readlines()
的任何调用都将读取所有行,直到文件末尾并将指针移动到文件末尾。
For 循环读取下一行(如果有的话)并不断重复该过程,直到指针位于文件末尾。在我们的代码中,这一行:
for index, l in enumerate(f, 1):
是一个 for 循环,它试图从文件中读取更多行。
不幸的是,在我们的例子中,通过调用 readlines()
,我们已经从文件中读取了所有行,并且已经将指针移动到文件末尾,因此 for 循环无法读入任何新行。
instances = ['XYZ']
curr_date = time.strftime("%Y%m%d")
for inst_name in instances:
with open(f'results_{inst_name}_master.csv') as f:
pkt_name = f.readline().split(';')[1]
print(f"package name is: {pkt_name}")
run_dt = f.readline().split(';')[1]
print(f"run date: {run_dt}")
time_check = f.readlines()[1].split(';')[-5].split(':')[0]
print(f"time is : {time_check}")
if ((run_dt == curr_date) or (run_dt < curr_date and time_check in range(20,24))):
for index, l in enumerate(f, 1):
if 'remoteInstallation' in l:
print(l)
示例文件:
pktName;sample-quality.zip;
RunDate;20220116;
sample:path_to_test\test;20220116;01:29:13;20220114;01:33:36;0:04:22;
;sample:path\to\remoteInstallation;45.99;../logs_path/index.html;OK;
;sample:path\to\remoteInstallation;42.87;../logs__other_path/index.html;1
我得到了除 print(l)
之外的所有结果,也就是说,它无法遍历文件,尽管文件中存在字符串:'remoteInstallation'。
请注意,实际上,文件在每行的末尾包含换行符(\n)。
我做错了什么?
提前致谢!
您似乎在 time_check 行上使用 readlines()
而不是 .readline()
。
import time
instances = ['XYZ']
curr_date = time.strftime("%Y%m%d")
for inst_name in instances:
with open(f'results_{inst_name}_master.csv') as f:
pkt_name = f.readline().split(';')[1]
print(f"package name is: {pkt_name}")
run_dt = f.readline().split(';')[1]
这行似乎不正确。它调用 readlines
并引用第一个索引处的元素。我删除了 s
和索引引用。
time_check = f.readline().split(';')[-5].split(':')[0]
print(f"time is : {time_check}")
if ((run_dt == curr_date) or (run_dt < curr_date and time_check in range(20,24))):
for index, l in enumerate(f, 1):
if 'remoteInstallation' in l:
print(l)
幕后发生了什么?
对于文件对象(在本例中 f
),有一个内部指针跟踪脚本当前在文件中查找的位置。这个指针是通用的,被多个函数和 for 循环使用。
每次调用 readline()
都会从文件中读取一行并将指针移动到下一行。
对 readlines()
的任何调用都将读取所有行,直到文件末尾并将指针移动到文件末尾。
For 循环读取下一行(如果有的话)并不断重复该过程,直到指针位于文件末尾。在我们的代码中,这一行:
for index, l in enumerate(f, 1):
是一个 for 循环,它试图从文件中读取更多行。
不幸的是,在我们的例子中,通过调用 readlines()
,我们已经从文件中读取了所有行,并且已经将指针移动到文件末尾,因此 for 循环无法读入任何新行。