2016-12-09 1 views
0

'paReference'에서 내 문장을 읽는 데 문제가 있습니다.Python이 dict에 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 

및 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,,,,, 

내가 잘못 paRefefence의 DICT에 CSV를 읽고있다 여기

enter image description here

는 123.csv입니까?

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 왜 부품 번호 하나만 발견되었지만 다른 부품 번호는 찾지 못했습니다. 어떤 제안이 도움이됩니다!

+0

'paReference = [item [0]] = [item [1] + item [2] .replace ('-', '')]'거의 당신이 원하는 것을하지 않습니다. 너는 무엇을하기를 원했는가? –

+0

첫 번째'if' 절에서 코드를 엉망이게 할 여분의 등호가 있습니다 :'paReference = [item [0]] = [item [1] + item [2] .replace ('-' , '')] – James

+0

키/값 쌍 컨테이너 https://docs.python.org/3.7/library/csv.html#csv에서 읽고 싶다면 사전을 만드는 대신'csv.DictReader'를 사용하십시오. DictReader – cmidi

답변

2

두 번째 csv의 productcode은 첫 번째 csv의 LineCodePartNo입니다. 당신이 구축 한 사전의 키에 대해 productcode을 체크하고있는 것 같습니다. 또한 제품 코드를 키로 사용하고 브랜드 이름을 값으로 사용하는 것처럼 보입니까? 그렇다면이 방법이 효과적입니다.

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]) 

의도 한 용도가 아닌 경우 의견을 게시하십시오.

+0

아, 예! 정말 고마워요! 이제는 완전히 이해가됩니다. –

관련 문제