私は列に別々のファイルベースを作成する必要があります。
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番目のスクリプトの作成について考えていました。
しかし、すべてが一番良いでしょう。
それはそれです... !!!私は "空のマッピング/辞書"を作成して、それに慣れていないでしょう。そして私はそれを理解し、それを理解すると思う。あなたのために二重の親指を! – Mobs