Re: Read csv file and create a new file

Discussion in 'Python' started by Neil Cerutti, Feb 28, 2013.

  1. Neil Cerutti

    Neil Cerutti Guest

    On 2013-02-28, io <> 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
     
    Neil Cerutti, Feb 28, 2013
    #1
    1. Advertising

  2. Neil Cerutti

    Dave Angel Guest

    On 02/28/2013 03:46 PM, io wrote:
    > Neil, it works great!
    >
    > Just one question : what can i do for ignoring the case sensitive of the
    > symbol?
    >
    > It wasn't working initially, then i wrote the values respecting case
    > sensitive in the file esclusioni and all worked as a charm. I would just
    > like to know if i could ignore the case senstive function.
    >
    > Thanks alot for your help.
    >
    > I'm in debt with you, i'll spend a pizza for you!
    >


    Just use a tolower() method on both strings when you're comparing them.
    Of course, that may not work well with international character sets.
    Some characters in some languages have no lowercase equivalent, and
    using toupper() has the same problem in other languages.

    Also, the approach to case insensitive differs between Python 2.x and 3.x


    --
    DaveA
     
    Dave Angel, Feb 28, 2013
    #2
    1. Advertising

  3. Neil Cerutti

    Dave Angel Guest

    On 02/28/2013 05:05 PM, io wrote:
    >
    >> Just use a tolower() method on both strings when you're comparing them.
    >> Of course, that may not work well with international character sets.
    >> Some characters in some languages have no lowercase equivalent, and
    >> using toupper() has the same problem in other languages.
    >>
    >> Also, the approach to case insensitive differs between Python 2.x and
    >> 3.x

    >
    > Thanks for the info !
    > What about str().lower()?
    >


    Actually, it's str.lower(), and that's what I meant. Must be some other
    language that uses tolower().

    --
    DaveA
     
    Dave Angel, Mar 1, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jliu66
    Replies:
    0
    Views:
    532
    jliu66
    Oct 19, 2007
  2. Li Chen
    Replies:
    18
    Views:
    688
    Azmi Farih
    Mar 23, 2010
  3. Neil Cerutti

    Re: Read csv file and create a new file

    Neil Cerutti, Feb 28, 2013, in forum: Python
    Replies:
    0
    Views:
    166
    Neil Cerutti
    Feb 28, 2013
  4. Dave Angel
    Replies:
    0
    Views:
    161
    Dave Angel
    Feb 28, 2013
  5. Dennis Lee Bieber

    Re: Read csv file and create a new file

    Dennis Lee Bieber, Mar 1, 2013, in forum: Python
    Replies:
    1
    Views:
    183
    Steven D'Aprano
    Mar 4, 2013
Loading...

Share This Page