2015-04-08 27 views
-2

我有以下格式的CSV文件:排序和重组CSV文件Python字典

ComponentID subComponent Measurement 
X030  A1111111  784.26 
X030  A2222222  784.26 
X015  A1111111  997.35 
X015  A2222222  997.35 
X015  A3333333  997.35 
X075  A1111111  673.2 
X075  A2222222  673.2 
X075  A3333333  673.2 
X090  A1111111  1003.2 
X090  A2222222  1003.2 
X090  A3333333  1003.2 
X105  A1111111  34.37 
X105  A2222222  34.37 
X105  A3333333  34.37 
X105  A4444444  34.37 

我想将文件还原为以下格式的Python字典:

my_dict = {'X030': ['A1111111', 'A2222222', 784.26], 
      'X015': ['A1111111', 'A2222222', 'A3333333', 997.35 ], 
      'X075': ['A1111111', 'A2222222', 'A3333333', 673.2], 
      'X090': ['A1111111', 'A2222222', 'A3333333', 1003.2], 
      'X105': ['A1111111', 'A2222222', 'A3333333', 'A4444444', 34.37] 
      } 

最初,我正在用itertools.groupby来看它,但那并没有让我到任何地方。我的困惑是如何因为我不知道如何返回下列项目设计它:ComponentID: [components, and only one measurement]

我不知道如何去这个任务,任何指导赞赏

+1

我想你至少确定这里的底层逻辑是什么,对吧?既然你没有共享任何代码,至少你可以分享这个预期的算法。 – fedorqui

+0

@fedorqui字典将提供一个外部类,用它来做一些计算和报告。 – dassouki

+0

我不是问“你将如何使用它”,而是“你将如何设计它”。像这样呈现,它看起来像是一份工作任务,而这应该是一个地方,让你展示你到目前为止所尝试的内容以及你被困在哪里。给https://docs.python.org读一读/2/library/csv.html#csv.DictReader – fedorqui

回答

1

我在理解数据结构时遇到了一些麻烦:是否保证任何给定ComponentID的所有子组件都具有相同的Measurement?如果是这样,那么给定的TSV格式和你想要的字典都不是存储这些信息的非常合理的数据结构。

尽管如此,这里是一些简单的代码,做你问什么:

d = {} 
with open('yourfile.tsv') as tsvfile: 
    next(tsvfile) 
    for line in tsvfile: 
    row = line.split() 
    componentid, subcomponent, measurement = row[0], row[1], float(row[2]) 
    if not componentid in d: 
     d[componentid] = [subcomponent, measurement] 
    else: 
     assert measurement == d[componentid][-1] 
     d[componentid] = d[componentid][:-1] + [subcomponent, measurement] 

,这里是一些把它在一个较为合理的结构代码:

d = {} 
with open('yourfile.tsv') as tsvfile: 
    next(tsvfile) 
    for line in tsvfile: 
    row = line.split() 
    componentid, subcomponent, measurement = row[0], row[1], float(row[2]) 
    if not componentid in d: 
     d[componentid] = {'subcomponents': [subcomponent], 'measurement': measurement} 
    else: 
     assert measurement == d[componentid]['measurement'] 
     d[componentid]['subcomponents'] += [subcomponent] 

这给你

{ 
    'X105': {'measurement': 34.37, 'subcomponents': ['A1111111', 'A2222222', 'A3333333', 'A4444444']}, 
    'X015': {'measurement': 997.35, 'subcomponents': ['A1111111', 'A2222222', 'A3333333']}, 
    'X075': {'measurement': 673.2, 'subcomponents': ['A1111111', 'A2222222', 'A3333333']}, 
    'X030': {'measurement': 784.26, 'subcomponents': ['A1111111', 'A2222222']}, 
    'X090': {'measurement': 1003.2, 'subcomponents': ['A1111111', 'A2222222', 'A3333333']} 
} 
+0

我修改了代码并使用以下函数解决了我的问题:/ def data_from_csv(csv_file):. '当d = {} 具有开放(csv_file)作为csvfile: 读者= csv.reader(csvfile,分隔符= '') 为行中的读取器: 的ComponentID,子组件,测量=行[0],行[ 1],浮子(行[2]) 如果d不COMPONENTID: d [的ComponentID] = [亚组分,测量] 否则: 断言测量== d [的ComponentID] [ - 1] d [的ComponentID] = d [componentid] [: - 1] + [subcomponent,measurement] return d' – dassouki

0

可以循环在你csv行和使用dict.setdefault方法的行存储在词典:

>>> import csv 
>>> d={} 
>>> with open('your_file.csv', newline='') as csvfile: 
...  spamreader = csv.reader(csvfile, delimiter='\t') 
...  for row in spamreader: 
...   d.setdefault(row[0],[]).extend(row[1:]) 
...  print d 
0

我的做法是:

myData = {} 
with open('p.csv') as inputfile: 
    for line in inputfile: 
     if ('ComponentID' not in line): 
      row = [x.strip() for x in line.split('  ')] 
      cid = row[0] 
      sub = row[1] 
      msmt = row[2] 

      if cid in myData.keys(): 
       msmt = myData[cid][-1] 
       myData[cid] = myData[cid][:-1] 
       myData[cid].append(sub) 
       myData[cid].append(msmt) 
      else: 
       myData[cid] = row[1:] 
print myData