我正在尝试遍历文件并查找是否存在条目。我需要搜索日期范围,不确定是先循环遍历文件中的行,然后再遍历每个日期,还是遍历日期然后在每一行中查找?
我尝试了两种选择,但是下面的代码似乎更“合乎逻辑”。我的问题是,为什么像程序员这样一个原因呢?为何下面的代码不尝试所有single_date
,而仅遍历文件中的所有行一次。
with open(r'reportLog.txt','r') as logFile:
for single_date in daterange(start_date, end_date):
for line in logFile:
if all(var in line for var in (reportName, str(single_date), 'R')):
print('found')
break
else:
print('not found')
reportLog.txt:
Digital_Incomplete_Leads,2019-05-10,12:15:29,12:15:29,Y
Digital_Incomplete_Leads,2019-05-09,12:15:43,12:15:43,Y
Account Movement Report,2019-05-06,13:54:07,13:54:12,Y
Account Movement Report,2019-05-07,13:54:07,13:54:12,Y
Account Movement Report,2019-05-08,13:54:07,13:54:12,Y
Account Movement Report,2019-05-09,13:53:38,13:53:38,R
Account Movement Report,2019-05-09,13:54:07,13:54:12,Y
我希望代码遍历文本文件并在找到以下行时退出:
Account Movement Report,2019-05-09,13:53:38,13:53:38,R
答案 0 :(得分:0)
为了提高速度,瓶颈几乎肯定会从文件中读取,因此应将其最小化。
您可以使用“ for logFile:中的行”作为外部循环来最小化行读取。
答案 1 :(得分:0)
关于使用哪个循环,事实是通常有不止一种方法来解决问题,几乎所有需要利用循环的编程问题都可以通过一种以上的循环来解决。考虑到效率,每种类型都有其优缺点。 本文将帮助您做出决定。 https://www.harrisgeospatial.com/Learn/Blogs/Blog-Details/ArtMID/10198/ArticleID/15332/What-Type-of-Loop-Should-I-Use
答案 2 :(得分:0)
文件:
Digital_Incomplete_Leads,2019-05-10,12:15:29,12:15:29,Y
Digital_Incomplete_Leads,2019-05-09,12:15:43,12:15:43,Y
Account Movement Report,2019-05-06,13:54:07,13:54:12,Y
Account Movement Report,2019-05-07,13:54:07,13:54:12,Y
Account Movement Report,2019-05-08,13:54:07,13:54:12,Y
Account Movement Report,2019-05-09,13:53:38,13:53:38,R
Account Movement Report,2019-05-09,13:54:07,13:54:12,Y
代码:
import csv
from datetime import datetime
start_date = datetime.strptime("2019-05-06", "%Y-%m-%d")
end_date = datetime.strptime("2019-05-08", "%Y-%m-%d")
with open('main_data.csv') as f:
csv_reader = csv.reader(f)
for idx, line in enumerate(csv_reader):
try:
d = datetime.strptime(line[1], "%Y-%m-%d")
if start_date <= d <= end_date:
print(f"Found \"{line[1]}\" in {idx} row.")
break
except ValueError:
print(f"Second column in {idx} row contain no date.")
except IndexError:
print(f"There's no second column in {idx} row.")
except:
print(f"Something unexpected in {idx} row.")
else:
print("Nothing have been found.")
输出:
Found "2019-05-06" in 2 row.
如果您想使用daterange()
这对我来说是未知的方法:
import csv
def daterange(start, end):
# unknown method #
pass
d_range = daterange("2019-05-06", "2019-05-08")
with open('main_data.csv') as f:
csv_reader = csv.reader(f)
for idx, line in enumerate(csv_reader):
try:
if line[1] in d_range:
print(f"Found \"{line[1]}\" in {idx} row.")
break
except ValueError:
print(f"Second column in {idx} row contain no date.")
except IndexError:
print(f"There's no second column in {idx} row.")
except:
print(f"Something unexpected in {idx} row.")
else:
print("Nothing have been found.")