我正在尝试简化或简化我的代码。 我想从内部服务器下载一个日志文件,该日志文件每10秒更新一次,但是我仅每10或15分钟运行一次脚本。 日志文件以分号分隔,并且其中有许多行我不使用。所以我的工作流程如下。
我是python的新手,如果您可以帮助我以更少的步骤来缩短/简化代码,我将不胜感激。
Context.getSystemService(String)
答案 0 :(得分:0)
您可能可以使用以下高级模块requests
。可以根据成功完成下载所花费的时间来增加超时时间。此外,两个with open
语句可以合并为一行。而且,为了将一行一行地加载到内存中,我们可以使用iter_lines
生成器。请注意,应设置stream=True
以便一次加载第一行。
from datetime import date
import csv
import requests
# Declare variables
today = str(date.today())
url = "http://localserver" + today + ".log"
outfile = 'output.log'
# Instead of waiting for 15 seconds explicitly consider using requests module
# with timeout parameter
response = requests.get(url, timeout=15, stream=True)
if response.status_code != 200:
print('Failed to get data:', response.status_code)
with open(outfile, 'w') as dest:
writer = csv.writer(dest)
# Walk through the request response line by line w/o loadin gto memory
line = list(response.iter_lines())[-1]
# Decode the response to string and split line by line
reader = csv.reader(line.decode('utf-8').splitlines(), delimiter=';')
# Read line by line for the splitted content and write to file
for r in reader:
writer.writerow((r[0], r[1], r[2], r[3], r[4], r[5], r[15], r[38], r[39], r[42], r[54], r[90], r[91], r[92],
r[111], r[116], r[121], r[122], r[123], r[124]))
print('File written successfully: ' + outfile)