小编All*_*ynH的帖子

在Python中解析大型的,可能是压缩的文件

我试图逐行解析一个大文件,以获取相关信息.我可能正在接收未压缩或gzip压缩文件(我可能需要在稍后阶段编辑zip文件).

我使用下面的代码,但我觉得,因为我不在with语句中,我不是逐行解析文件,实际上是将整个文件加载file_content到内存中.

if ".gz" in FILE_LIST['INPUT_FILE']:
    with gzip.open(FILE_LIST['INPUT_FILE']) as input_file:
        file_content = input_file.readlines()
else:
    with open(FILE_LIST['INPUT_FILE']) as input_file:
        file_content = input_file.readlines()

for line in file_content:
    # do stuff
Run Code Online (Sandbox Code Playgroud)

有关如何处理此问题的任何建议?我宁愿不在代码块之外解压缩文件,因为这需要是通用的,我将不得不整理多个文件.

python gzip python-2.7

5
推荐指数
1
解决办法
205
查看次数

Python 将日期时间设置为 UTC 时区

我有一个事件,我想在 Python 中触发,在周五早上和周日早上之间的每个周末。

我已经编写了一些适用于我的本地环境的代码,但我担心在部署到生产时,日期时间将被本地化并且触发器将不正确。理想情况下,我希望所有内容都同步到 UTC,这是我的尝试 - 我想看看它是否正确,以及是否有人对如何使其更清洁有反馈。

(代码对我有用,但无论如何我都在正确的时区:))

from datetime import datetime
def eventTrigger():
    if((datetime.weekday(datetime.today()) == 4) and (datetime.now().utcnow.hour) > 9):
        return True
    elif ((datetime.weekday(datetime.today()) == 6) and (datetime.now().utcnow.hour) < 10):
        return True
    elif (datetime.weekday(datetime.today()) == 5):
        return True
    else:
        return False
Run Code Online (Sandbox Code Playgroud)

我尝试阅读日期时间文档,但它非常令人困惑。

python timezone datetime python-2.7

2
推荐指数
2
解决办法
5932
查看次数

标签 统计

python ×2

python-2.7 ×2

datetime ×1

gzip ×1

timezone ×1