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

    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Beginner at python, need help


    I have a program that reads a CSV file. It currently looks like this. The CSV file is NFL Football stats. The names of the rows of the CSV are Name, Games, Receptions, Receiving Yards, etc.



    import csv

    players = {}

    with open('NFL 2011 reassembled.csv', 'rU') as csvfile:
    spamreader = csv.DictReader(csvfile, delimiter=',', quotechar='|')
    for row in spamreader:
    players[row['Name'].strip()] = row



    The numbers are strings now, so I cannot change them. How can I change them into integers?
    Thanks. I am a beginner to Python, so any help would be greatly appreciated.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480

    Use int


    >>> print(7*int('4'))
    28
    [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
    2
    Rep Power
    0
    That won't work because I want to be able to put any row of the CSV file in there. Is there something I could do that would look something like:

    int(players['Games')
    #players is the name of the csv file
    Or something that would turn it into an integer?
    thanks
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    [QUOTE=123456789876543]That won't work because I want to


    >>> a = "545.2222"
    >>> float(a)
    545.22220000000004
    >>> int(float(a))
    545


    Try this. If still not working, please get back.

    Regards,
    Sachin Bali
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    If you provide a few lines of the input and also show what you want for output, and perhaps a formula (in some sort of reasonable descriptive language) that computes the output from the input, you might thereby get some useful assistance.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo