CSVをPythonの複数の辞書にインポートしたい
queryInclude,yahoo,value1
queryInclude,yahoo,value2
queryInclude,yahoo,value3
queryExclude,yahoo,value4
queryExclude,yahoo,value5
queryInclude,google,value6
queryExclude,google,value7
私の理想的な結果は、row [0] =辞書、row [1] =キー、row[2]=値または値のリストになります。
queryInclude = {
"yahoo": ["value1", "value2", "value3"],
"google": ["value6"] }
queryExclude = {
"yahoo": ["value4", "value5"],
"google": ["value7"] }
これが私のコードです:
import csv
queryList=[]
queryDict={}
with open('dictionary.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
queryDict[row[1]] = queryList.append(row[2])
print queryDict
{'yahoo': None}
{'yahoo': None}
{'yahoo': None}
{'yahoo': None}
{'yahoo': None}
{'google': None, 'yahoo': None}
{'google': None, 'yahoo': None}
必要に応じてCSV形式を柔軟に変更できます。上に投稿した私の理想的な結果は、すでにアプリにハードコーディングしたものです。今後、さらに価値を追加しやすくしようとしています。私はこれを研究するのに何時間も費やしました、そして私がさらに進歩するならば更新し続けるでしょう。私の思考プロセスは次のようになります...CSV行を反復処理しながら、ループを構造化し、同様の値を組み合わせる方法を理解するのにどれだけ近づいているかわかりません...
for row in reader:
where row[0] = queryInclude:
create a dictionary combining keys into a list of values
where row[0] = queryExclude:
create a dictionary combining keys into a list of values