2017-01-19 97 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