2017-01-19 11 views
0

私は列に別々のファイルベースを作成する必要があります。
1つの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 

私はそれを分離し、その後、この最終的なリストを取る2番目のスクリプトの作成について考えていました。
しかし、すべてが一番良いでしょう。

答えて

0

これは、辞書と2つの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

関連する問題