0

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!

4
  • paReference =[item[0]] = [item[1] + item[2].replace('-','')] is almost certainly not doing what you want. What did you want it to do? Commented Dec 9, 2016 at 15:43
  • You have an extra equal sign that is going to mess up your code in the first if clause here: paReference =[item[0]] = [item[1] + item[2].replace('-','')] Commented Dec 9, 2016 at 15:43
  • use csv.DictReader rather than creating your dictionary if you want to read in a key/value pair container docs.python.org/3.7/library/csv.html#csv.DictReader Commented Dec 9, 2016 at 15:46
  • Ah yes I see that now. But untilamtely I am trying to concatenate LineCode and Part number and use THAT as a reference for d ('abc.csv'). Commented Dec 9, 2016 at 15:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes! awesome thank you so much! Makes total sense now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.