#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Dictionary Text and numbers


    Hi

    I have a script that is looking up multiple files, the files have a consistent layout they are a notepad from a CSV file. In some files there is a number and in some files there is a text. i have created a dictionary that one of the items is: {'A':float (data[-1][5]).
    The issue is when there is a number it is fine and it recognizes the number however when there is a text say "Car" it returns a error. Do I need to write a If/Elif statement to get around this and if I could get some assistance on what it would need to say that would be handy

    ta

    Jezza
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,901
    Rep Power
    481
    Testing the data if elif is fine. Another way via error trapping, try except
    Code:
    >>> try:
    ...     float('car')
    ... except ValueError:
    ...     # Anticipated
    ...
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Follow up with whole code


    Hi here is the whole script
    import csv
    with open("Drive.txt", "rb") as csvfile:
    reader = csv.reader(csvfile, delimiter="\t")
    data = list(reader)
    dictionary = {'C':float (data[-1][8]),'E':float (data[-1][9]), 'G':float (data[-1][11]),'J':float (data[-1][14])}
    Letter = raw_input("Enter Dealer ")
    print str('Price $'+"%.2f" % (dictionary[Letter]))


    However if when you enter C for the file above and it says Does not sell in that cell it won't run it comes with the value error, I entered what you specified and it still errored

    Jezza
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,901
    Rep Power
    481

    At considerable self-risk, something like this might work.


    Code:
    def cvt(s,default=-1):
        try:
            return float(s)
        except ValueError:
            return default
    
    import csv
    with open("Drive.txt", "rb") as csvfile:
        reader = csv.reader(csvfile, delimiter="\t")
        data = list(reader)
    dictionary = {'C':cvt (data[-1][8]),'E':cvt (data[-1][9]), 'G':cvt (data[-1][11]),'J':cvt (data[-1][14])}
    Letter = raw_input("Enter Dealer ")
    print str('Price $'+"%.2f" % (dictionary[Letter]))
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0
    Thanks b49P23TIvg is their any way I can get it to return what the text was in the box for example if it says no car that is the response if not what you hhave replied is sufficent for me to continue on

    Jezza
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,901
    Rep Power
    481
    Yes, of course you'd return the original text rather than the default. Then you'll probably have errors later when you try to format a string with %.2f

    >>> '%.2f'%'car'
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: float argument required, not str
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo