「データ」と「マッピング」の 2 つの CSV ファイルがあります。
- 「マッピング」ファイルには、、、、、の 4 つの列
Device_Name
がGDN
ありDevice_Type
ますDevice_OS
。4 つの列すべてが入力されます。 - 「データ」ファイルにはこれらと同じ列があり、列にデータ
Device_Name
が入力され、他の 3 つの列は空白になっています。 - Python コードで両方のファイルを開き
Device_Name
、データ ファイルのそれぞれに対して、そのGDN
、Device_Type
、およびDevice_OS
値をマッピング ファイルからマッピングする必要があります。
2 列しか存在しない場合 (1 列をマップする必要がある場合) dict を使用する方法は知っていますが、3 列をマップする必要がある場合にこれを達成する方法がわかりません。
以下は、のマッピングを達成しようとしたコードですDevice_Type
。
x = dict([])
with open("Pricing Mapping_2013-04-22.csv", "rb") as in_file1:
file_map = csv.reader(in_file1, delimiter=',')
for row in file_map:
typemap = [row[0],row[2]]
x.append(typemap)
with open("Pricing_Updated_Cleaned.csv", "rb") as in_file2, open("Data Scraper_GDN.csv", "wb") as out_file:
writer = csv.writer(out_file, delimiter=',')
for row in csv.reader(in_file2, delimiter=','):
try:
row[27] = x[row[11]]
except KeyError:
row[27] = ""
writer.writerow(row)
返しますAttribute Error
。
いくつかの調査の後、ネストされた辞書を作成する必要があると思いますが、これを行う方法がわかりません。