Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39323

Re: Import Json web data source to xls or csv

From io <maroso@libero.it>
Subject Re: Import Json web data source to xls or csv
Newsgroups comp.lang.python
References <51240f54$0$40361$4fafbaef@reader1.news.tin.it> <kg1s92$5df$1@dont-email.me>
Date 2013-02-20 08:19 +0000
Message-ID <51248722$0$40358$4fafbaef@reader1.news.tin.it> (permalink)
Organization TIN.IT (http://www.tin.it)

Show all headers | View raw


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"])])

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Import Json web data source to xls or csv io <maroso@libero.it> - 2013-02-19 23:48 +0000
  Re: Import Json web data source to xls or csv Michael Herman <hermanmu@gmail.com> - 2013-02-19 17:54 -0800
    Re: Import Json web data source to xls or csv io <maroso@libero.it> - 2013-02-20 07:13 +0000
      Re: Import Json web data source to xls or csv Michael Herman <hermanmu@gmail.com> - 2013-02-20 10:27 -0800
    Re: Import Json web data source to xls or csv rusi <rustompmody@gmail.com> - 2013-02-20 23:29 -0800
      Re: Import Json web data source to xls or csv Peter Otten <__peter__@web.de> - 2013-02-21 09:36 +0100
  Re: Import Json web data source to xls or csv Cousin Stanley <cousinstanley@gmail.com> - 2013-02-20 06:59 +0000
    Re: Import Json web data source to xls or csv io <maroso@libero.it> - 2013-02-20 08:19 +0000
      Re: Import Json web data source to xls or csv pyplexed <julius.welby@gmail.com> - 2013-02-20 02:47 -0800
        Re: Import Json web data source to xls or csv io <maroso@libero.it> - 2013-02-21 00:27 +0000

csiph-web