Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # test.py
- import csv
- import collections
- index = collections.defaultdict(list)
- file1= open( "someFile.csv", "rb" )
- rdr= csv.DictReader( file1 )
- for row in rdr:
- index[rdr['MPID']].append( row )
- file1.close()
- file2= open( "anotherFile.csv", "rb" )
- rdr= csv.DictReader( file2 )
- for row in rdr:
- print row, index[row['MPID']]
- file2.close()
- # someFile.csv
- MPID,Title,Description,Model,Category ID,Category Description,Subcategory ID,Subcategory Description,Manufacturer ID,Manufacturer Description,URL,Manufacturer (Brand) URL,Image URL,AR Price,Price,Ship Price,Stock,Condition
- 4,"Foo","Bar","FooBar",13,"Thirteen",4,"Four",7,"Seven","http://foobar.com/foobar",,"$4.00","$5.00","$10.00","$5.00",4,"New"
- # anotherFile.csv
- Regular Price,Sale Price,Manufacturer Name,Model Number,Retailer Category,Buy URL,Product Name,Availability,Shipping Cost,Condition,MPID,Image URL,UPC,Description
- "$5.00","$4.00","FooBar","M-18-45","Soap","htpp://foobar.com/buy?M-18-45","Restful SOAP",4,"$5.00","New",4,,"7777-999-3","Beauty Soap"
- # python2.6 test.py
- # Traceback (most recent call last):
- # File "test.py", line 8, in <module>
- # index[rdr['MPID']].append( row )
- # AttributeError: DictReader instance has no attribute '__getitem__'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement