2017-01-19 57 views
0

我需要根据列创建单独的文件。
从一个csv列创建多个文件。 Python 2.7.12

我从源代码#1获取数据。
然后发送数据到源#2,但源#2只识别第2列的代码。
我可以取数据并替换代码。

testdata.csv

1|b|430418886 
1|f|434324988 
1|c|445454512 
1|f|430418574 
1|a|432343445 
1|d|437657654 
1|e|424328828 
1|a|430236546 
1|e|434565445 
1|c|430418988 
1|d|430420012 
1|b|476556568 

codelist.csv

a|171 
b|172 
c|173 
d|174 
e|176 
f|177 

我可以创建完整的列表,但我无法基于代码的文件分开。
这组文件看起来像这样。

171.csv 
1|171|432343445 
1|171|430236546 

172.csv 
1|172|430418886 
1|172|476556568 

173.csv 
1|173|445454512 
1|173|430418988 

174.csv 
1|174|437657654 
1|174|430420012 

176.csv 
1|176|424328828 
1|176|434565445 

177.csv 
1|177|434324988 
1|177|430418574 

在这里,我的代码,目前为止,创建完整列表。

def get_site_code_dict(site_code_file): 
    mydict = dict() 
    with open(site_code_file) as inputs: 
     for line in inputs: 
      name, code = line.strip().split("|") 
      mydict[name] = code 
    return mydict 

def process_raw(raw_file, site_code_dict): 
    with open(raw_file) as inputs, open('ouput.csv', 'w') as outlist: 
     for line in inputs: 
      active, code, idnumber = line.strip().split("|") 
      outlist.write("1"+'|') 
      outlist.write(site_code_dict[code]+'|') 
      outlist.write(idnumber+'\n') 
    outlist.close() 


if __name__ == "__main__": 
    site_code_dict = get_site_code_dict("codelist.csv") 
    process_raw("testdata.csv", site_code_dict) 

输出:

1|172|430418886 
1|177|434324988 
1|173|445454512 
1|177|430418574 
1|171|432343445 
1|174|437657654 
1|176|424328828 
1|171|430236546 
1|176|434565445 
1|173|430418988 
1|174|430420012 
1|172|476556568 

我在想创造希望借此最终名单第二个脚本,然后将其分离。
但是,一切都将是最好的。

回答

0

这是一个常见的模式,可以用字典和两个for循环来解决。可以通过一个共同的属性分组元素在一起时,可以使用此模式,在这种情况下,code

代码背后的想法是:

1)创建的字典,可以通过代码组一切

2)通过的所有记录环和通过最终的词典和输出每码的信息的所有键到词典

3)循环添加信息

def get_site_code_dict(site_code_file): 
    mydict = dict() 
    with open(site_code_file) as inputs: 
     for line in inputs: 
      name, code = line.strip().split("|") 
      mydict[name] = code 
    return mydict 

def process_raw(raw_file, site_code_dict): 
    code_to_instances = {} # set up out empty mapping/dictionary 
    with open(raw_file) as inputs: 
     for line in inputs: 
      active, letter, idnumber = line.strip().split("|") 
      code = site_code_dict[letter] 
      if code not in code_to_instances: # if the code hasn't yet been added to the dict 
       code_to_instances[code] = [] # Create an entry with a blank list of instances 
      code_to_instances[code].append({ 'active': active, 'id': idnumber }) # Add the element to the list 

    for code in code_to_instances.keys(): # for each code 
     with open(code + '.csv', 'w') as outlist: # open a file with a named based on the code 
      for instance in code_to_instances[code]: # for each instance 
       outlist.write(instance['active'] +'|') # write the instance information per line 
       outlist.write(code +'|') 
       outlist.write(instance['id'] +'\n')  

if __name__ == "__main__": 
    site_code_dict = get_site_code_dict("codelist.csv") 
    process_raw("testdata.csv", site_code_dict) 
+0

就是这样...... !!!我永远不会那样做,创建一个“空映射/字典”。我想我理解它,经历它。双赞赞你! – Mobs