CSV中二维列表中的Python组值

时间:2018-10-11 09:19:13

标签: python python-3.x list grouping ordereddict

我有以下CSV

BBCP1,Grey,2140,805EC0FFFFE2,0000000066
BBCP1,Test,2150,805EC0FFFFE2,0000000066
BBCP1,Test,2151,805EC0FFFFE1,0000000066
BBCP1,Centre,2141,805EC0FFFFE3,000000077
BBCP1,Yellow,2142,805EC0FFFFE3,000000077
BBCP1,Purple,2143,805EC0FFFFE3,000000077
BBCP1,Green,2144,805EC0FFFFE3,000000077
BBCP1,Pink,2145,805EC0FFFFE3,000000077

我正在使用

读取此数据
data = list(csv.reader(open(csvFile)))

我想将此数据转换为2d数组或等效数据,并按第4列(MAC地址)中的值分组,保留其顺序在原始列表中。所以看起来像

[(BBCP1,Grey,2140,805EC0FFFFE2,0000000066),(BBCP1,Test,2150,805EC0FFFFE2,0000000066)],
[(BBCP1,Test,2151,805EC0FFFFE1,0000000066)],
[(BBCP1,Centre,2141,805EC0FFFFE3,000000077),
(BBCP1,Yellow,2142,805EC0FFFFE3,000000077),
(BBCP1,Purple,2143,805EC0FFFFE3,000000077),
(BBCP1,Green,2144,805EC0FFFFE3,000000077),
(BBCP1,Pink,2145,805EC0FFFFE3,000000077)]

希望我已经正确显示了数组,这很有意义。

然后我需要循环数组以将数据输出到文件。我很确定我可以使用嵌套的for循环。

在此先感谢您的帮助

2 个答案:

答案 0 :(得分:2)

使用defaultdict对数据进行分组(groupby将需要排序,并且效率不高/会取消订单),然后打印已排序的字典值(排序实际上不是必需的,只是为了稳定输出):

import csv,collections

d = collections.defaultdict(list)

for row in csv.reader(txt):
    mac_address = row[3]
    d[mac_address].append(row)

print(sorted(d.values()))

导致:

[[['BBCP1', 'Centre', '2141', '805EC0FFFFE3', '000000077'],
  ['BBCP1', 'Yellow', '2142', '805EC0FFFFE3', '000000077'],
  ['BBCP1', 'Purple', '2143', '805EC0FFFFE3', '000000077'],
  ['BBCP1', 'Green', '2144', '805EC0FFFFE3', '000000077'],
  ['BBCP1', 'Pink', '2145', '805EC0FFFFE3', '000000077']],
 [['BBCP1', 'Grey', '2140', '805EC0FFFFE2', '0000000066'],
  ['BBCP1', 'Test', '2150', '805EC0FFFFE2', '0000000066']],
 [['BBCP1', 'Test', '2151', '805EC0FFFFE1', '0000000066']]]

根据密钥(mac地址)排序:

values = [v for _,v in sorted(d.items())]

产量:

[[['BBCP1', 'Test', '2151', '805EC0FFFFE1', '0000000066']],
 [['BBCP1', 'Grey', '2140', '805EC0FFFFE2', '0000000066'],
  ['BBCP1', 'Test', '2150', '805EC0FFFFE2', '0000000066']],
 [['BBCP1', 'Centre', '2141', '805EC0FFFFE3', '000000077'],
  ['BBCP1', 'Yellow', '2142', '805EC0FFFFE3', '000000077'],
  ['BBCP1', 'Purple', '2143', '805EC0FFFFE3', '000000077'],
  ['BBCP1', 'Green', '2144', '805EC0FFFFE3', '000000077'],
  ['BBCP1', 'Pink', '2145', '805EC0FFFFE3', '000000077']]]

答案 1 :(得分:1)

嗨,我用pandasgroupby解决了这个问题。希望这会有所帮助!

data = pd.read_csv('data.txt', header=None)
data.columns = ['A','B','C','D','E'] # random names to the column

def check(data):
    data_item = []
    for index,item in data.iterrows():
        data_item.append(item.tolist()))
    return data_item   

grouped_data = data.groupby('D',sort=False).apply(check)

for data in grouped_data:
    print(data)

输出#保留订单

[['BBCP1', 'Grey', 2140, '805EC0FFFFE2', 66], ['BBCP1', 'Test', 2150, '805EC0FFFFE2', 66]]
[['BBCP1', 'Test', 2151, '805EC0FFFFE1', 66]]
[['BBCP1', 'Centre', 2141, '805EC0FFFFE3', 77], ['BBCP1', 'Yellow', 2142, '805EC0FFFFE3', 77], ['BBCP1', 'Purple', 2143, '805EC0FFFFE3', 77], ['BBCP1', 'Green', 2144, '805EC0FFFFE3', 77], ['BBCP1', 'Pink', 2145, '805EC0FFFFE3', 77]]