Import Json web data source to xls or csv
io
maroso at libero.it
Wed Feb 20 03:19:47 EST 2013
Il Wed, 20 Feb 2013 06:59:46 +0000, Cousin Stanley ha scritto:
> io wrote:
>
>> ....
>> How do i manage to read the data source from
>> http://bitcoincharts.com/t/markets.json ....
>> I just need currency, symbol, bid, ask, volume ....
>
> Following is a simple way load the json data and write the desired
> fields to a .csv file
>
>
> import json import urllib
>
> url = "http://bitcoincharts.com/t/markets.json"
>
> response = urllib.urlopen( url ) ;
>
> data = json.loads( response.read() )
>
> list_dicts = [ dict( this ) for this in data ]
>
> f = open( 'markets.csv' , 'w' )
>
> for this_dict in list_dicts :
>
> currency = str( this_dict[ 'currency'] )
> symbol = str( this_dict[ 'symbol' ] )
> bid = str( this_dict[ 'bid' ] )
> ask = str( this_dict[ 'ask' ] )
> volume = str( this_dict[ 'volume' ] )
>
> this_list = [ currency , symbol , bid , ask , volume ]
>
> this_str = ','.join( this_list )
>
> f.write( this_str + '\n' )
>
> f.close()
Thanks Stanley!
That's another nice code to use!
I was trying to sort out some condition but can't get this working :
import json
import urllib
import csv
url = "http://bitcoincharts.com/t/markets.json"
response = urllib.urlopen(url);
data = json.loads(response.read())
f = open("bitcoin.csv","wb")
c = csv.writer(f)
# write headers
c.writerow(["Currency","Symbol","Bid", "Ask", "Volume"])
#if str(d["bid"])and str(d["ask"])[0] not in ( 'none' ):
for d in data where str(d["bid"])and str(d["ask"])[0] not in ( 'none' ):
c.writerow([str(d["currency"]),str(d["symbol"]),str(d["bid"]),str(d
["ask"]),str(d["currency_volume"])])
More information about the Python-list
mailing list