python读取用空格分隔的文字_在python 2.7中打开以空格(?)分隔的文本文件?...

1586010002-jmsa.png

I have what I think is a space delimited text file that I would like to open and copy some of the data to lists (Python 2.7). This is a snippet of the data file:

0.000000 11.00 737.09 1.00 1116.00

0.001000 14.00 669.29 10.00 613.70

0.002000 15.00 962.27 2.00 623.50

0.003000 7.00 880.86 7.00 800.71

0.004000 9.00 634.67 3.00 1045.00

0.005000 12.00 614.67 3.00 913.33

0.006000 12.00 782.58 6.00 841.00

0.007000 13.00 860.08 6.00 354.00

0.008000 14.00 541.07 4.00 665.25

0.009000 14.00 763.00 6.00 1063.00

0.010000 9.00 790.33 6.00 857.83

0.011000 6.00 899.83 4.00 1070.75

0.012000 16.00 710.88 10.00 809.90

0.013000 12.00 863.50 7.00 923.14

0.014000 9.00 591.67 6.00 633.17

0.015000 12.00 740.58 6.00 837.00

0.016000 10.00 727.60 7.00 758.00

0.017000 12.00 838.75 4.00 638.75

0.018000 9.00 991.33 7.00 731.57

0.019000 12.00 680.75 5.00 1079.40

0.020000 15.00 843.20 3.00 546.00

0.021000 11.00 795.18 5.00 1317.20

0.022000 9.00 943.33 5.00 911.00

0.023000 13.00 711.23 3.00 981.67

0.024000 11.00 922.73 5.00 1111.00

0.025000 1112.00 683.58 6.00 542.83

0.026000 15.00 1053.80 5.00 1144.40

Below is the code I have tried, which does not work. I would like to have two lists, one each from the second and the fourth column.

listb = []

listd = []

with open('data_file.txt', 'r') as file:

reader = csv.reader(file,delimiter=' ')

for a,b,c,d,e in reader:

listb.append(int(b))

listd.append(int(d))

What am I doing wrong?

解决方案

The problem is the multiple spaces between fields (columns).

CSV stands for comma-separated values. Imagine for a second that you are using commas instead of spaces. Line 1 in your file would then look like:

,,,,0.000000,,,,,,,11.00,,,,,,737.09,,,,,,,1.00,,,,,1116.00

So, the CSV reader sees more than 5 fields (columns) in that row.

You have two options:

Switch to using single space separators

Use a simple split() to deal with multiple whitespace:

:

listb = []

listd = []

with open('text', 'r') as file:

for row in file:

a, b, c, d, e = row.split()

listb.append(int(b))

listd.append(int(d))

P.S: Once this part is working, you will run into a problem calling int() on strings like "11.00" which aren't really integers.

So I recommend using something like:

int(float(b))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值