在python 2.7中打开以空格(?)分隔的文本文件吗? [英] Opening a space(?) delimited text file in python 2.7?

查看:362
本文介绍了在python 2.7中打开以空格(?)分隔的文本文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以空格分隔的文本文件,我想打开该文件并将某些数据复制到列表中(Python 2.7).这是数据文件的一个片段:

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))  

我在做什么错了?

推荐答案

问题是字段(列)之间存在多个空格.

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

CSV代表逗号分隔的值.想象一下,您正在使用逗号而不是空格.文件中的第1行如下所示:

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

因此,CSV阅读器在该行中看到了5个以上的字段(列).

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

您有两个选择:

  1. 切换为使用单个空格分隔符
  2. 使用简单的split()处理多个空格:

:

 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:一旦这部分工作了,您将遇到一个问题,例如不是真正的整数的字符串,例如int(11.00)上调用int(). 所以我建议使用类似的东西:

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))

这篇关于在python 2.7中打开以空格(?)分隔的文本文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆