Python CSV reader return Row as list -
im trying parse csv using python , able index items in row can accessed using row[0]
, row[1]
, on.
so far code:
def get_bitstats(): url = 'http://bitcoincharts.com/t/trades.csv?symbol=mtgoxusd' data = urllib.urlopen(url).read() dictreader = csv.dictreader(data) obj = bitdata() row in dictreader: obj.datetime = datetime.datetime.fromtimestamp(int(row['0'])/1000000) q = db.query(bitdata).filter('datetime', obj.datetime) if q != none: raise valueerror(obj.datetime + 'is in database') else: obj.price = row['1'] obj.amount = row['2'] obj.put()
this returns keyerror: '0'
, have no idea how set up. did input interactive shell , when running
for row in dictreader: print row
i output:
{'1': '3'} {'1': '6'} {'1': '2'} {'1': '6'} {'1': '9'} {'1': '8'} {'1': '6'} {'1': '4'} {'1': '4'} {'1': '', none: ['']} {'1': '4'} {'1': '2'} {'1': '.'} {'1': '0'} {'1': '5'} {'1': '7'} {'1': '1'} {'1': '6'} {'1': '0'} {'1': '0'} {'1': '0'} {'1': '0'} {'1': '0'} {'1': '0'} {'1': '0'} {'1': '', none: ['']} {'1': '0'} {'1': '.'} {'1': '0'} {'1': '1'} {'1': '0'} {'1': '0'} {'1': '5'} {'1': '4'} {'1': '2'} {'1': '5'} {'1': '0'} {'1': '0'} {'1': '0'} {'1': '0'} {'1': '1'} {'1': '3'} {'1': '6'} {'1': '2'} {'1': '6'} {'1': '9'} {'1': '8'} {'1': '6'} {'1': '4'} {'1': '4'}
and on , on thousands , thousands of lines. ( im sure csv thousands of digits)
why csv printing way , there anyway separate row list of 3 ints
such [130534543, 47.00009, 23001.9000]
edit:
as answer states using wrong csv function in code above though fixing gave me list list in same format dict such that:
['1'] ['2'] ['1'] ['3'] ['8'] ['3'] ['5'] . . .
it turns out had remove .read()
data = urllib.urlopen(url).read()
.
csv.reader
return each row list
reader = csv.reader(data) line_list in reader: pass # line_list list of data contained in row can access line_list[0]
Comments
Post a Comment