user5811454
user5811454

Reputation:

Python reading csv into dict

Im having trouble reading my dict in 'paReference'.

enter image description here

here is 123.csv:

BrandName,LineCode,PartNo,ShortDescription,SuperStock,DropshipStock,Price,Core
Carter,5C,M3643,FILTER,0,0,14.8,0
Carter,5C,M3805,FILTER,1,0,14.8,0
Carter,5C,M4512,FILTER,2,0,14.8,0
Carter,5C,M4642,FILTER,3,0,14.8,0
Carter,5C,M60088,FILTER,4,0,14.8
Carter,5C,M60142,FILTER,6,0,14.8
Carter,5C,M60167,FILTER,7,0,14.8,0

and abc.csv:

productcode,book_isbn,stockstatus,Price,Core,AddtoCartBtn_Replacement_Text,Availability
5CM2195,5CM2195,,,,,
5CM2846,5CM2846,,,,,
5CM3643,5CM3643,,,,,
5CM3805,5CM3805,,,,,
5CM4512,5CM4512,,,,,
5CM4642,5CM4642,,,,,
5CM60088,5CM60088,,,,,
5CM60142,5CM60142,,,,,
5CM60167,5CM60167,,,,,

Am I reading the csv into the paRefefence dict improperly?

c = csv.reader(open(pluginPath + "123.csv"))
d = csv.reader(open(pluginPath + "abc.csv"))

paReference ={}
for item in c:
    if item[1] not in paReference:
        paReference =[item[0]] = [item[1] + item[2].replace('-','')]
    else:
        paReference[item[1]].append(item[0])
    print(item)

for item in d:
    if item[0] not in paReference:
        print ("Not Found! " + item[0])
    else:
        print ("Found! " + item[0])

Im new to python I'm having trouble understanding why it has found one part number but not the others. Any suggestions help!

Upvotes: 0

Views: 159

Answers (1)

James
James

Reputation: 36756

It looks like the productcode from the 2nd csv is the LineCode plus PartNo from the first csv. It looks like you are checking the productcode against the keys of the dictionary you built. It also looks like you are using the productcode as the keys and the brand name as the value? If that is the case, this should work.

paReference = {}
for item in c:
    pcode = item[1] + item[2].replace('-','')
    if pcode not in paReference:
        paReference[pcode] = [item[0]]
    else:
        paReference[pcode].append(item[0])
    print(item)

for item in d:
    if item[0] not in paReference:
        print ("Not Found! " + item[0])
    else:
        print ("Found! " + item[0])

If this is not your intended use, post a comment.

Upvotes: 1

Related Questions