[Python] np.loadtxt skips unexpected lines when reading strings

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have a file structured as such:

    IID SuperPops
    FGXXXXXXXX R6
    FGXXXXXXXX R12


    Containing 524123 lines.

    I need to read the first column as an array:

    len(np.loadtxt('foo.txt',usecols=0,dtype=str))
    524123


    But if i also try to skip the header all of a sudden 10 lines go missing:

    len(np.loadtxt('foo.txt',usecols=0,dtype=str,skiprows=1))
    524112


    This does not happen if i change type though:

    len(np.loadtxt('foo.txt',usecols=0,dtype='<U20',skiprows=1))
    524122
    len(np.loadtxt('foo.txt',usecols=0,dtype='<U10',skiprows=1))
    524122


    The 10 missing entries are all of length 10:

    [len(elem) for elem in set(np.loadtxt('foo.txt',usecols=0,dtype='<U20',skiprows=1)) - set(np.loadtxt('foo.txt',usecols=0,dtype=str,skiprows=1))]
    [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]


    What could be the reason and why is it specifically triggered by skiprows?

    Continue reading...

Compartilhe esta Página