繁体   English   中英

在 python 中读取大 txt 文件的有效方法

[英]Efficient way of reading large txt file in python

我正在尝试打开一个包含 4605227 行 (305 MB) 的 txt 文件

我以前这样做的方式是:

data = np.loadtxt('file.txt', delimiter='\t', dtype=str, skiprows=1)

df = pd.DataFrame(data, columns=["a", "b", "c", "d", "e", "f", "g", "h", "i"])

df = df.astype(dtype={"a": "int64", "h": "int64", "i": "int64"})

但它用尽了大部分可用内存 ~10GB 并且没有完成。 有没有更快的方法来读取这个 txt 文件并创建 pandas dataframe?

谢谢!

编辑:现在解决了,谢谢。 为什么 np.loadtxtx() 这么慢?

与其使用 numpy 读取它,不如直接将其读取为 Pandas DataFrame。 例如,使用pandas.read_csv function,类似于:

df = pd.read_csv('file.txt', delimiter='\t', usecols=["a", "b", "c", "d", "e", "f", "g", "h", "i"])

方法一:

您可以按块读取文件,此外还有一个缓冲区大小,您可以在 readline 中提及并且您可以读取。

inputFile = open('inputTextFile','r')
buffer_line = inputFile.readlines(BUFFERSIZE)
while buffer_line:
    #logic goes here

方法二:

您也可以使用 nmap 模块,下面是解释用法的链接。

导入地图

with open("hello.txt", "r+b") as f:
    # memory-map the file, size 0 means whole file
    mm = mmap.mmap(f.fileno(), 0)
    # read content via standard file methods
    print(mm.readline())  # prints b"Hello Python!\n"
    # read content via slice notation
    print(mm[:5])  # prints b"Hello"
    # update content using slice notation;
    # note that new content must have same size
    mm[6:] = b" world!\n"
    # ... and read again using standard file methods
    mm.seek(0)
    print(mm.readline())  # prints b"Hello  world!\n"
    # close the map
    mm.close()

https://docs.python.org/3/library/mmap.html

您直接将其读取为 Pandas DataFrame。 例如

import pandas as pd
pd.read_csv(path)

如果你想更快地阅读,你可以使用 modin:

import modin.pandas as pd
pd.read_csv(path)

https://github.com/modin-project/modin

下面的代码将逐行读取文件,它将在 for 循环中遍历文件 object 中的每一行并根据需要处理这些行。

with open("file.txt") as fobj:

for line in fobj:

    print(line) #do your process

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM