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

标签 python

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

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/

相关文章:

python - 为什么 `function` 不是 Python 中的关键字?

python - gtk idle_add 没有运行?

python 正则表达式 : re. findall(r"(do|re|mi) +","mimi rere midore")

python - 可选参数初始化中的访问类

python - MySQL 中的数字序列

python - 每次调用 next() 时如何用 python 返回多行?

javascript - 带有 JSON 数据的 D3 线,未渲染

python - 将数据附加到波形声音文件而不加载其当前内容

python - 在python中使用zip函数时出错

Python:使用正则表达式捕获模式中的子模式