使用python将文件中的列转换为单独的列表

Converting columns in a file into separate lists using python

我有一个Excel文件,我想阅读每一列并为它们创建单独的列表。然后,我想将每个列表的每个元素与我拥有的其他对应列表进行比较。有没有一种方法可以使用python将列转换为列表?

例如(我可以把这个文件作为一个txt空间分隔文件或Excel文件)

1
2
3
4
5
6
7
8
9
HST_9578_02_ACS_WFC_F775W 245.8976441 -26.5255957 4339.570 1882.364
HST_10615_03_ACS_WFC_F435W 245.8976450 -26.5255138 2084.978 2101.122
HST_10120_02_ACS_WFC_F658N 245.8976758 -26.5255024 1778.055 1752.193
HST_10775_64_ACS_WFC_F606W 245.8977532 -26.5255296 2586.612 2603.519
HST_10775_64_ACS_WFC_F814W 245.8977532 -26.5255296 2586.612 2603.519
HST_9578_02_ACS_WFC_F775W 245.8978148 -26.5255491 4328.571 1885.712
HST_10120_02_ACS_WFC_F625W 245.8978053 -26.5254741 1769.711 1754.229
HST_10353_02_ACS_WFC_F435W 245.8976003 -26.5257784 3758.430 985.125
HST_10775_64_ACS_WFC_F606W 245.8979115 -26.5254936 2576.410 2606.114

这听起来像是python的csv模块的工作。每一行读取都作为字符串列表返回。

从文档中借用一个简短的示例:

Each row read from the csv file is returned as a list of strings. No
automatic data type conversion is performed.

A short usage example:

[这只是打印出每一行]

1
2
3
4
5
import csv
with open('some.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        print row

您可以通过对具有适当索引值的行进行索引来访问特定的列。

或者,如果您想"手动"执行此操作(每行由一个,分隔):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
s ="""HST_9578_02_ACS_WFC_F775W 245.8976441 -26.5255957 4339.570 1882.364,
HST_10615_03_ACS_WFC_F435W 245.8976450 -26.5255138 2084.978 2101.122,
HST_10120_02_ACS_WFC_F658N 245.8976758 -26.5255024 1778.055 1752.193,
HST_10775_64_ACS_WFC_F606W 245.8977532 -26.5255296 2586.612 2603.519,
HST_10775_64_ACS_WFC_F814W 245.8977532 -26.5255296 2586.612 2603.519,
HST_9578_02_ACS_WFC_F775W 245.8978148 -26.5255491 4328.571 1885.712,
HST_10120_02_ACS_WFC_F625W 245.8978053 -26.5254741 1769.711 1754.229,
HST_10353_02_ACS_WFC_F435W 245.8976003 -26.5257784 3758.430 985.125,
HST_10775_64_ACS_WFC_F606W 245.8979115 -26.5254936 2576.410 2606.114
"""


bl = [[],[],[],[],[]]
for r in s.split(','):
    for c in range(5):
        bl[c].append(r.split()[c])

给予:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bl[0]
['HST_9578_02_ACS_WFC_F775W', 'HST_10615_03_ACS_WFC_F435W', 'HST_10120_02_ACS_WFC_F658N', 'HST_10775_64_ACS_WFC_F606W', 'HST_10775_64_ACS_WFC_F814W', 'HST_9578_02_ACS_WFC_F775W', 'HST_10120_02_ACS_WFC_F625W', 'HST_10353_02_ACS_WFC_F435W', 'HST_10775_64_ACS_WFC_F606W']

bl[1]
['245.8976441', '245.897645', '245.8976758', '245.8977532', '245.8977532', '245.8978148', '245.8978053', '245.8976003', '245.8979115']

bl[2]
['-26.5255957', '-26.5255138', '-26.5255024', '-26.5255296', '-26.5255296', '-26.5255491', '-26.5254741', '-26.5257784', '-26.5254936']

bl[3]
['4339.57', '2084.978', '1778.055', '2586.612', '2586.612', '4328.571', '1769.711', '3758.43', '2576.41']

bl[4]
['1882.364', '2101.122', '1752.193', '2603.519', '2603.519', '1885.712', '1754.229', '985.125', '2606.114']

编辑/更新:

将这两种方法合并为一种方法:

1
2
3
4
5
6
7
8
import csv

with open('so.csv') as f:
    bl = [[],[],[],[],[]]
    reader = csv.reader(f)
    for row in reader:
        for col in range(5):
            bl[col].append(row[col])

使用with打开文件的好处是,当您完成操作或发生异常时,它将自动为您关闭。


一个线性版本:

1
2
with open('go.txt') as input:
    print zip(*(line.split() for line in input))

或用CSV

1
2
with open('go.txt') as input:
     print zip(*csv.reader(input, delimiter = ' '))

zip(*)最终完成了所需的转换,将每一列转换成自己的列表。zip进行相反的转换,将列组合成一个列表。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
>>> s ="""HST_9578_02_ACS_WFC_F775W 245.8976441 -26.5255957 4339.570 1882.364
HST_10615_03_ACS_WFC_F435W 245.8976450 -26.5255138 2084.978 2101.122
HST_10120_02_ACS_WFC_F658N 245.8976758 -26.5255024 1778.055 1752.193
HST_10775_64_ACS_WFC_F606W 245.8977532 -26.5255296 2586.612 2603.519
HST_10775_64_ACS_WFC_F814W 245.8977532 -26.5255296 2586.612 2603.519
HST_9578_02_ACS_WFC_F775W 245.8978148 -26.5255491 4328.571 1885.712
HST_10120_02_ACS_WFC_F625W 245.8978053 -26.5254741 1769.711 1754.229
HST_10353_02_ACS_WFC_F435W 245.8976003 -26.5257784 3758.430 985.125
HST_10775_64_ACS_WFC_F606W 245.8979115 -26.5254936 2576.410 2606.114
"""

>>> from collections import defaultdict
>>> cols = defaultdict(list)
>>> for line in s.split('
'
):
    for index, val in enumerate(line.split()):
        cols[index].append(val)


>>> cols
defaultdict(<type 'list'>, {0: ['HST_9578_02_ACS_WFC_F775W', 'HST_10615_03_ACS_WFC_F435W', 'HST_10120_02_ACS_WFC_F658N', 'HST_10775_64_ACS_WFC_F606W', 'HST_10775_64_ACS_WFC_F814W', 'HST_9578_02_ACS_WFC_F775W', 'HST_10120_02_ACS_WFC_F625W', 'HST_10353_02_ACS_WFC_F435W', 'HST_10775_64_ACS_WFC_F606W'], 1: ['245.8976441', '245.8976450', '245.8976758', '245.8977532', '245.8977532', '245.8978148', '245.8978053', '245.8976003', '245.8979115'], 2: ['-26.5255957', '-26.5255138', '-26.5255024', '-26.5255296', '-26.5255296', '-26.5255491', '-26.5254741', '-26.5257784', '-26.5254936'], 3: ['4339.570', '2084.978', '1778.055', '2586.612', '2586.612', '4328.571', '1769.711', '3758.430', '2576.410'], 4: ['1882.364', '2101.122', '1752.193', '2603.519', '2603.519', '1885.712', '1754.229', '985.125', '2606.114']})
>>> cols[0]
['HST_9578_02_ACS_WFC_F775W', 'HST_10615_03_ACS_WFC_F435W', 'HST_10120_02_ACS_WFC_F658N', 'HST_10775_64_ACS_WFC_F606W', 'HST_10775_64_ACS_WFC_F814W', 'HST_9578_02_ACS_WFC_F775W', 'HST_10120_02_ACS_WFC_F625W', 'HST_10353_02_ACS_WFC_F435W', 'HST_10775_64_ACS_WFC_F606W']
>>>

1
2
3
4
5
with open('data.txt') as f:
   lis=[x.split() for x in f]
cols=[x for x in zip(*lis)]
for x in cols:
    print(x)

输出:

1
2
3
4
5
 ('HST_9578_02_ACS_WFC_F775W', 'HST_10615_03_ACS_WFC_F435W', 'HST_10120_02_ACS_WFC_F658N', 'HST_10775_64_ACS_WFC_F606W', 'HST_10775_64_ACS_WFC_F814W', 'HST_9578_02_ACS_WFC_F775W', 'HST_10120_02_ACS_WFC_F625W', 'HST_10353_02_ACS_WFC_F435W', 'HST_10775_64_ACS_WFC_F606W')
('245.8976441', '245.8976450', '245.8976758', '245.8977532', '245.8977532', '245.8978148', '245.8978053', '245.8976003', '245.8979115')
('-26.5255957', '-26.5255138', '-26.5255024', '-26.5255296', '-26.5255296', '-26.5255491', '-26.5254741', '-26.5257784', '-26.5254936')
('4339.570', '2084.978', '1778.055', '2586.612', '2586.612', '4328.571', '1769.711', '3758.430', '2576.410')
('1882.364', '2101.122', '1752.193', '2603.519', '2603.519', '1885.712', '1754.229', '985.125', '2606.114')