Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 22nd, 2013  |  syntax: None  |  size: 0.65 KB  |  views: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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
clone this paste RAW Paste Data