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


Groups > comp.lang.python > #40168

Re: Read csv file and create a new file

From Neil Cerutti <neilc@norwich.edu>
Newsgroups comp.lang.python
Subject Re: Read csv file and create a new file
Date 2013-02-28 20:11 +0000
Organization Norwich University
Message-ID <ap9s02Fuf0kU1@mid.individual.net> (permalink)
References <512fac9c$0$40355$4fafbaef@reader1.news.tin.it> <512fb533$0$40355$4fafbaef@reader1.news.tin.it>

Show all headers | View raw


On 2013-02-28, io <maroso@libero.it> wrote:
> I'm a noob in python but my code looks like this :
>
>
> import json
> import urllib
> import csv

I take back what I said about the csv module. It appears you need
access to at least one of the data fields, so this is a good use
of csv.

> url = "http://bitcoincharts.com/t/markets.json"
> response = urllib.urlopen(url);
> data = json.loads(response.read())
>
> f = open("/home/io/markets.csv","wb")
> c = csv.writer(f)
>
> #apre un file di testo e legge il contenuto del file inserendolo in una 
> stringa
> esclusioni = open('/home/io/exclusions.txt','r')
> string = ""

The list of exclusions should be stored in a set or list, not a
string. This is your main "bug."

esclusioni_file = open('/home/io/exclusions.txt','r')
esclusioni = []

> while 1:
>     line = esclusioni.readline()
>     if not line:break
>     string += line
> print string

Iterate over the file instead of looping manually.

 for line in esclusioni_file:
     esclusioni.append(line.strip())
 print(esclusioni)

> # write headers
> c.writerow(["Currency","Symbol","Bid", "Ask", "Volume"])
>
> for d in data:
>     if d["currency"] <> "SLL":  #esclude la valuta di secondlife SLL
>         if d["bid"] is not None and d["ask"] is not None:
>             if not any(str(d["symbol"]) in s for s in string):

Why are you checking d["symbol"] instead of d["currency"]? Maybe
I misunderstood the question.

Test like this for either set or list container type. Use
whichever json field is appropriate:

              if d["currency"] not in esclusioni:

>                 c.writerow([str(d["currency"]),str(d["symbol"]),str(d
> ["bid"]),str(d["ask"]),str(d["currency_volume"])])
>     
> esclusioni.close()

-- 
Neil Cerutti

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


Thread

Read csv file and create a new file io <maroso@libero.it> - 2013-02-28 19:14 +0000
  Re: Read csv file and create a new file Neil Cerutti <neilc@norwich.edu> - 2013-02-28 19:32 +0000
  Re: Read csv file and create a new file Joel Goldstick <joel.goldstick@gmail.com> - 2013-02-28 14:34 -0500
  Re: Read csv file and create a new file Dave Angel <davea@davea.name> - 2013-02-28 14:35 -0500
  Re: Read csv file and create a new file io <maroso@libero.it> - 2013-02-28 19:51 +0000
    Re: Read csv file and create a new file Neil Cerutti <neilc@norwich.edu> - 2013-02-28 20:11 +0000
      Re: Read csv file and create a new file io <maroso@libero.it> - 2013-02-28 20:23 +0000
        Re: Read csv file and create a new file io <maroso@libero.it> - 2013-02-28 20:46 +0000
          Re: Read csv file and create a new file io <maroso@libero.it> - 2013-02-28 20:51 +0000
          Re: Read csv file and create a new file Dave Angel <davea@davea.name> - 2013-02-28 16:23 -0500
            Re: Read csv file and create a new file io <maroso@libero.it> - 2013-02-28 22:05 +0000
              Re: Read csv file and create a new file Dave Angel <davea@davea.name> - 2013-02-28 19:13 -0500
  Re: Read csv file and create a new file Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-28 21:04 -0500
    Re: Read csv file and create a new file io <maroso@libero.it> - 2013-03-04 19:04 +0000
      Re: Read csv file and create a new file Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-04 23:58 +0000

csiph-web