SHARE
TWEET

Untitled

a guest Oct 22nd, 2013 29 Never
  1. jaslkfdj,asldkfj,,,
  2.  slakj,aklsjf,,,
  3.  lsak,sajf,,,
  4.        
  5. newdict= {}
  6. wd = open('file.csv', 'r')
  7. for line in wd:
  8.       key,val = line.split(',')
  9.       newdict[key]=val
  10. print(newdict)
  11.        
  12. >>> line = 'jaslkfdj,asldkfj,,,'
  13. >>> key, value = line.split(',')
  14. ValueError: too many values to unpack (expected 2)
  15.        
  16. >>> line = 'jaslkfdj,asldkfj,,,'
  17. >>> parts = line.split(',')
  18. >>> key, value = parts[:2]
  19.        
  20. >>> line = 'jaslkfdj,asldkfj,,,'
  21. >>> key, value = line.split(',', 1)
  22.        
  23. >>> value = value.rstrip(',')
  24.        
  25. "a,b,,,,,".split(",")[:2]
  26.        
  27. with open('file.csv', 'r') as wd:
  28.     newdict = dict(line.split(",")[:2] for line in wd.read().splitlines())
  29.  
  30. print newdict
RAW Paste Data
Top