简体   繁体   中英

Numpy genfromtxt skipping leading whitespace in each line

import numpy as np

stuff ='       57107 1 1589 1 2201-1 1 1 1-1 111-1-1-1   3991555 1 2 1 1 695 0A65 -1 1 1 1 2-1-1 24546-1-1  -1 -1-1-1-1-1-1 2 1-155- 1 2 1 2 6 240 1 8 9 1 1-1 1 1 1 2 1 2 57 57 57 1 0 0 2 1 1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 2-170-1-1 70-1-1-1-1 2-1 14080-1 80-1  697 2 5 5-1 2 3 5 2-1-1-1-1-1-1 -1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 -1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 1-122 1 1 1 6-1-1-1 -1 1-1 1 2-1-1-1-1-1-1-1 6 4840678-1-1 -1 -1 2-1-1-1 2 4-1-1 6-143-135-1 117-1 9-1 3 1 1 1-1-1-1-1-1-1  -1  -1  -1  0-1-1      0-1-1-1      -1      0-1-1-1-1-1-1-1-1-1-1-1-1-1   3991555         0         0   3642944   3958306-1    -1-1 0 0 0 1 0-1 050 0 0 0 011 0 0 0-1-1-1-1 0 0-1 1 1 0 1 0 1 1 0 1 1 1 0 1 0 1 1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 0 0 0-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1\n       57107 1 1589 1 2201-1 1 1 1-1 111-1-1-1   3991555 1 2 1 1 695 0A65 -1 1 1 1 2-1-1 24546-1-1  -1 -1-1-1-1-1-1 2 3-154- 1 1 2 2 6 239 1 8 9 1 2-1 1 2 1 2 1 2 57 57 57 1 0 0 1 1 1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 1 22420-1 44-1-1-1-1 2-1 2-12420 44-1   -1 1 5 5-1 1 3 5 2-1-1-1-1-1-1 -1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 -1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 1-119 1 1 1 2-1-1-1 -1 1-1 1 2-1-1-1-1-1-1-1 4 2630265-1-1 -1 -1 2-1-1-1 2 4-1 1 6-134-119-1 110-1 4-1 1 1 1 2-1-1-1-1-1-1  -1  -1  -1  0-1-1      0-1-1-1      -1      0-1-1-1-1-1-1-1-1-1-1-1-1-1   3991555         0         0   3991555   3998325-1    -1-1 0 0 0 1 0-1 050 0 0 0 011 0 0 0-1-1-1-1 0 0-1 1 1 0 0 0 0 1 0 1 1 1 0 0 0 1 1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 0 0 0-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1\n       57108 1  912 1 1201-1 2 1 1-1 1 2-1-1-1   4478084 1 1 6 1 695 0A65 -1 1 2-1-1-1-1 24546-1-1  -1 -1-1-1-1-1-1 2 2-122- 6-1 1 2 6 240 1 8 9 0 1-1 0 0 2 2 7 2 57 57 57 1 0 0 1 1 1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 2-140-1-1 40-1-1-111 125 2-115-1 15-1  321 2 5 5-1 2 3 3 2-1-1-1-1-1-1 -1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 -1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 1-114 1 1 1 4-1 011 -1 1-1 1 4-1-1-1-1-1-1-1 2 4850889-1-1 -1 -1 2-1-1-1 2 2-1 2 8-144-142-1 118-112-1 3-1 1 1-1-1-1-1-1-1  -1  -1  -1  0-1-1      0-1-1-1      -1      0-1-1-1-1-1-1-1-1-1 1 2 2 2   4478084         0         0   4478084   4609610-1    -1-1 0 0 0 1 0-1 050 0 0 0 011 0 0 0-1-1-1-1 0 0-1 1 1 0 1 0 1 1 0 1 1 1 0 1 0 0 1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 0 0 0-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 0 0 0'
filename = 'test.dat'
writer = open(filename, mode='w')
writer.write(stuff)
writer.close()

data = np.genfromtxt(filename,delimiter = ',', dtype=None, encoding='utf-8')

Running the above code returns 3 rows with each line of text, however it skips the whitespace in the beginning of each line. How do I include it?

The variables have different positions and lengths (ex. variable A is at position 32 with length 2 and variable B is at position 68 with length 1), so it matters that the whitespace is included so that I can parse the string correctly.

I am also stuck with using numpy genfromtxt because the files I work with sometimes have null characters (I've tried using pd.read_csv(filename, dtype=None, encoding='utf-8')). When using read_csv, pandas treats the null characters as end of line terminators, so I lose all the text after the null chars.

Using numpy loadtxt instead of genfromtxt fixed the issue.

EDIT: Upon advice from @hpaulj, using filename.readlines() would be better suitable for what I'm trying to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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