Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 6th, 2012  |  syntax: None  |  size: 3.14 KB  |  hits: 10  |  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. Reading in CSV file to add data to a dictionary or tuple
  2. Open-r CVS
  3.     Open-rwb main
  4.     While (cvs)
  5.             Readline(CVS)
  6.             Readline (main)
  7.       If cvs == main
  8.             Add to record
  9.             Wright (main)
  10.       else
  11.       If cvs > main
  12.             Readline (main)
  13.       else
  14.       If cvs < main
  15.             Readline (cvs)
  16.        
  17. AAC,D,20111207,9.83,9.83,9.83,9.83,100
  18. AACC,D,20111207,3.46,3.47,3.4,3.4,13400
  19. AACOW,D,20111207,0.3,0.3,0.3,0.3,500
  20. AAME,D,20111207,1.99,1.99,1.95,1.99,8600
  21. AAON,D,20111207,21.62,21.9,21.32,21.49,93200
  22. AAPL,D,20111207,389.93,390.94,386.76,389.09,10892800
  23.        
  24. AACC,D,20120127,4.01,4.02,4.03,4.04,40000
  25. B,D,20120127,4.01,4.02,4.03,4.04,40000
  26.        
  27. import csv
  28. mainReader = csv.DictReader( open("m.csv.txt","rb"), ["id"],"others")
  29.  
  30. newReader = csv.DictReader( open("d.csv.txt","rb"), ["id"],"others")
  31. stocks = {}
  32. for line in mainReader:
  33.     stocks[line['id']] = line
  34.  
  35. print stocks   # output hand-formatted
  36. {
  37.    'AAC':  {'id':'AAC',
  38.             'others': ['D','20111207','9.83','9.83','9.83','9.83','100']},
  39.    'AACC': {'id':'AACC',
  40.             'others': ['D', '20111207', '3.46', '3.47', '3.4', '3.4', '13400']},
  41.    'AAME': {'id': 'AAME',
  42.             'others': ['D', '20111207', '1.99', '1.99', '1.95', '1.99', '8600']},
  43.    'AACOW': {'id': 'AACOW',
  44.             'others': ['D', '20111207', '0.3', '0.3', '0.3', '0.3', '500']},
  45.    'AAPL': {'id': 'AAPL', 'others':
  46.             ['D','20111207','389.93','390.94','386.76','389.09','10892800']},
  47.    'AAON': {'id': 'AAON',
  48.             'others': ['D', '20111207', '21.62', '21.9', '21.32', '21.49', '93200']}
  49. }
  50.        
  51. for line in newReader:
  52.     # can use if line['id'] in stocks: to check for an existing record if desired
  53.     # in this example, we blindly overwrite any existing entry
  54.     stocks[line['id']] = line
  55.  
  56. print stocks    # output hand-formatted
  57. {
  58.     'AAME': {'id': 'AAME',
  59.              'others': ['D', '20111207', '1.99', '1.99', '1.95', '1.99', '8600']},
  60.     'B': {'id': 'B',
  61.           'others': ['D', '20120127', '4.01', '4.02', '4.03', '4.04', '40000']},
  62.     'AACC': {'id': 'AACC',
  63.              'others': ['D', '20120127', '4.01', '4.02', '4.03', '4.04', '40000']},
  64.     'AAPL': {'id': 'AAPL',
  65.         'others': ['D','20111207','389.93','390.94','386.76','389.09','10892800']},
  66.     'AAON': {'id': 'AAON',
  67.         'others': ['D', '20111207', '21.62', '21.9', '21.32', '21.49', '93200']},
  68.     'AACOW': {'id': 'AACOW',
  69.         'others': ['D', '20111207', '0.3', '0.3', '0.3', '0.3', '500']}
  70. }
  71.        
  72. s = stocks.items()
  73. s.sort()
  74. print s   # output hand-formatted
  75. [
  76.     'AACC': {'id': 'AACC',
  77.              'others': ['D', '20120127', '4.01', '4.02', '4.03', '4.04', '40000']},
  78.     'AACOW': {'id': 'AACOW',
  79.         'others': ['D', '20111207', '0.3', '0.3', '0.3', '0.3', '500']},
  80.     'AAME': {'id': 'AAME',
  81.              'others': ['D', '20111207', '1.99', '1.99', '1.95', '1.99', '8600']},
  82.     'AAON': {'id': 'AAON',
  83.         'others': ['D', '20111207', '21.62', '21.9', '21.32', '21.49', '93200']},
  84.     'AAPL': {'id': 'AAPL',
  85.         'others': ['D','20111207','389.93','390.94','386.76','389.09','10892800']},
  86.     'B': {'id': 'B',
  87.           'others': ['D', '20120127', '4.01', '4.02', '4.03', '4.04', '40000']}
  88. ]