简体   繁体   中英

python reading file with multiple delimiters

Another newbie in python.

I am trying to read from file the following

000001 001,01 001,02 001,03 001,04 +83.44556 -4.42692396 +.0018912 -1.625533 +.0006944 -85.36787 +367.192620 +369.0000 20180130_141607 CH 01 GN 1 128

000002 001,01 001,02 001,04 001,05 +54.27095 -.719790446 +.0004728 -.2643017 +.0001736 +8.225120 +367.192620 +369.0000 20180130_141607 CH 02 GN 2 128

I tried the follwing command

Z0=np.genfromtxt('1X21_DD_WN20180130_1114.Data',comments='#','!','R'],
skip_header=100,skip_footer=2)

but the number 001,01 001,02 are returned as NAN.

I tried the

Z00=np.genfromtxt('1X21_DD_WN20180130_1114.Data',comments=
['#','!','R'],skip_header=100,skip_footer=2,delimiter=[' ',','])

But I get an error message (Cannt perform accumulate with flexible type)

Then I went to the following path

with open('1X21_DD_WN20180130_1114.Data', 'rb') as f:
    y=[[     line.replace(b',',b' ')    ] for line in f.readlines()[100:-3]]

which returns a list in y. But I do not know how to proceed with the split.

Now I am using the following expression

in_file=open('1X21_DD_WN20180130_1416.Data', 'rb')
readfile=in_file.readlines()[100:-3]
y2=np.zeros([17,1])
for line in readfile:
    y2=np.c_[y2,np.asarray(line.replace(b',',b' ').split())
    [0:17].astype(float)];

y2=y2[:,1:]
y2=y2.T  

There must be a better way Any help?

Anyhow, with some trial and error, I come to this

with open('1X21_DD_WN20180130_1416.Data', 'rb') as f:
    y=np.asarray([     line.replace(b',',b' ').replace(b'*',b'0').replace(b'TX',b'0').replace(b'Resist.',b'0').replace(b'out',b'0').replace(b'of',b'0').replace(b'range',b'0').split()[0:17]    for line in f.readlines()[100:-3]]).astype(float)

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