gpt4 book ai didi

python - 我想使用编解码器操作列

转载 作者:太空宇宙 更新时间:2023-11-03 14:33:58 25 4
gpt4 key购买 nike

我正在尝试从压缩文件中提取信息。我使用了这段代码:

import codecs
!gunzip pagecounts-20150501-000000.gz
log = codecs.open('pagecounts-20150501-000000', 'r')
lines = log.readlines(1)
print(lines)

但是它只给我文件中的第一个字母。该文件实际上有 4 列,但我想使用数据框或任何其他结构来存储海量数据(几乎 186328237 行)的前两列。

最佳答案

您只要求第一行中的第一个字符:

lines = log.readlines(1)

您仅将“1”传递给readlines'read() method - 如果您想读取更多内容,请更改该值,或者您可以使用 readline() 方法一次读取一行。

来自 docs :

readlines([sizehint[, keepends]]) Read all lines available on the input stream and return them as a list of lines.

Line-endings are implemented using the codec’s decoder method and are included in the list entries if keepends is true.

sizehint, if given, is passed as the size argument to the stream’s read() method.

要阅读第一行,请尝试:

lines = log.readline()

关于python - 我想使用编解码器操作列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47098459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com