Advertisement
Guest User

Python CSV module __getitem__ issue

a guest
Aug 6th, 2010
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. # test.py
  2. import csv
  3. import collections
  4.  
  5. index = collections.defaultdict(list)
  6. file1= open( "someFile.csv", "rb" )
  7. rdr= csv.DictReader( file1 )
  8. for row in rdr:
  9.     index[rdr['MPID']].append( row )
  10. file1.close()
  11.  
  12. file2= open( "anotherFile.csv", "rb" )
  13. rdr= csv.DictReader( file2 )
  14. for row in rdr:
  15.     print row, index[row['MPID']]
  16. file2.close()
  17.  
  18. # someFile.csv
  19. 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
  20. 4,"Foo","Bar","FooBar",13,"Thirteen",4,"Four",7,"Seven","http://foobar.com/foobar",,"$4.00","$5.00","$10.00","$5.00",4,"New"
  21.  
  22. # anotherFile.csv
  23. Regular Price,Sale Price,Manufacturer Name,Model Number,Retailer Category,Buy URL,Product Name,Availability,Shipping Cost,Condition,MPID,Image URL,UPC,Description
  24. "$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"
  25.  
  26. # python2.6 test.py
  27. # Traceback (most recent call last):
  28. #   File "test.py", line 8, in <module>
  29. #     index[rdr['MPID']].append( row )
  30. # AttributeError: DictReader instance has no attribute '__getitem__'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement