python - 可以在循环内多次使用 getline() 吗? - Cython,文件读取

标签 python cython getline stdio file-read

我想读取一个 4 行 4 行的文件(它是一个带有 DNA 序列的 fastq 文件)。
当我一行一行或一行两行读取文件时,没有问题,但是当我一次读取 3 或 4 行时,我的代码崩溃了(内核似乎在 jupyter notebook 上死掉了)。 (取消注释最后一部分,或 4 个 getline() 中的任意 3 个。
我尝试使用双字符数组(char**)来存储行,但存在同样的问题。
知道是什么原因吗?
使用 Python 3.7.3、Cython 0.29,更新了所有其他库。正在读取的文件大约为 1.3GB,机器有 8GB,ubuntu 16.04。
代码改编自 https://gist.github.com/pydemo/0b85bd5d1c017f6873422e02aeb9618a

%%cython
from libc.stdio cimport FILE, fopen, fclose, getline
    
def fastq_reader(early_stop=10):
    cdef const char* fname = b'/path/to/file'
    cdef FILE* cfile
    cfile = fopen(fname, "rb")

    cdef:
        char * line_0 = NULL
        char * line_1 = NULL
        char * line_2 = NULL
        char * line_3 = NULL
        size_t seed = 0
        ssize_t length_line
        unsigned long long line_nb = 0

    while True:
        length_line = getline(&line_0, &seed, cfile)
        if length_line < 0: break
        
        length_line = getline(&line_1, &seed, cfile)
        if length_line < 0: break
        
#         length_line = getline(&line_2, &seed, cfile)
#         if length_line < 0: break
        
#         length_line = getline(&line_3, &seed, cfile)
#         if length_line < 0: break

        line_nb += 4
        if line_nb > early_stop:
            break

    fclose(cfile)
    return line_nb

fastq_reader(early_stop=20000)

最佳答案

根本问题是我对 getline() 的误解getline() c reference
要将行存储在不同的变量中,关联的 n每个行指针都需要*lineptr .

If *lineptr is set to NULL and *n is set 0 before the call, then getline() will allocate a buffer for storing the line.


Alternatively, before calling getline(), *lineptr can contain a pointer to a malloc(3)-allocated buffer *n bytes in size. If the buffer is not large enough to hold the line, getline() resizes it with realloc(3), updating *lineptr and *n as necessary.

n (或 seed 在我的代码中)将保存为指针分配的缓冲区的大小,其中 getline() 放置传入行。当我为不同的指针设置相同的缓冲区变量时,getline 得到了 char* line_xxx 大小的错误信息。

由于 fastq 文件通常是这种形状:
@read_id_usually_short
CTATACCACCAAGGCTGGAAATTGTAAAACACACCGCCTGACATATCAATAAGGTGTCAAATTCCCTTTTCTCTAGCTTTCGTACT_very_long
+
-///.)/.-/)//-//..-*...-.&%&.--%#(++*/.//////,/*//+(.///..,%&-#&)..,)/.,.._same_length_as_line_2
一两个具有相同缓冲区长度的 getline() 没有错误,因为缓冲区太小并且 getline 增大了指针的大小。
但是当使用 3 或 4 个 getlines() 时,调用 length_line = getline(&line_2, &seed, cfile)被要求存储长度为 2 ('+\n') 的 char*,同时得到 ( 错误信息 ) 指针 line_2已经足够大(line_1 的大小)。

所以(简单的)解决方案是
%%cython
from libc.stdio cimport FILE, fopen, fclose, getline
    
def fastq_reader(early_stop=10):
    cdef const char* fname = b'/path/to/file'
    cdef FILE* cfile
    cfile = fopen(fname, "rb")

    cdef:
        char * line_0 = NULL
        char * line_1 = NULL
        char * line_2 = NULL
        char * line_3 = NULL
        # One variable for each line pointer
        size_t n_0 = 0
        size_t n_1 = 0
        size_t n_2 = 0
        size_t n_3 = 0
        ssize_t length_line
        unsigned long long line_nb = 0

    while True:
        # Reading the same file (same cfile), but line_x and n_x by pairs)
        length_line = getline(&line_0, &n_0, cfile)  
        if length_line < 0: break
        
        length_line = getline(&line_1, &n_1, cfile)
        if length_line < 0: break
        
        length_line = getline(&line_2, &n_2, cfile)
        if length_line < 0: break
        
        length_line = getline(&line_3, &n_3, cfile)
        if length_line < 0: break

        line_nb += 4
        if line_nb > early_stop:
            break

    fclose(cfile)
    return line_nb

fastq_reader(early_stop=20000)
谢谢你指出我的错误。

关于python - 可以在循环内多次使用 getline() 吗? - Cython,文件读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67257960/

相关文章:

javascript - Twilio 客户端 Python 在 IOS 浏览器中不工作

Python pandas groupby 在多列上聚合,然后旋转

python - django:按字母顺序对表数据进行排序

c++ - 有没有办法只获取一行的一部分?

c++ - 检查 string::getline 中的 eof

python - 在 Jupyter Notebook 中调用 JS 文件并共享数据

python - Cython:如何按另一个 vector 对一个 vector 的内容进行排序?

python - (Cython) 在 __pow__ 中使模参数可选

c - 3D vector ,返回指针还是返回结构?

c++ getline()在多次调用时不等待来自控制台的输入