Reading data from a file and manipulating it

This is the place for queries that don't fit in any of the other categories.

Reading data from a file and manipulating it

Postby thegripmaster » Sat Jun 14, 2014 11:02 am

I have a test file called sampledata.csv in the format name, age, height

Code: Select all
adam, 32, 175
lisa, 22, 170
brian, 33, 180
oxana, 40, 177
billy, 20, 189


I need a simple python script to read the file and tell me:

1. the average age
2. the average height
3. the tallest person
4. the shortest person

Any help will be appreciated. Thanks.
Last edited by stranac on Sat Jun 14, 2014 11:33 am, edited 1 time in total.
Reason: First post lock.
thegripmaster
 
Posts: 2
Joined: Sat Jun 14, 2014 10:59 am

Re: Reading data from a file and manipulating it

Postby stranac » Sat Jun 14, 2014 11:36 am

You can use the csv module to read the data and do the calculations yourself, or you could use something like pandas, which will do most of the work for you.
Try writing some code, and feel free to ask questions if you get stuck.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: Reading data from a file and manipulating it

Postby thegripmaster » Sat Jun 14, 2014 10:07 pm

Thanks but I was actually wanting to see how difficult it will be solving this problem with Python. And no, I just want to keep it simple and not want to use the csv module. I am just looking for a simple solution.
thegripmaster
 
Posts: 2
Joined: Sat Jun 14, 2014 10:59 am

Re: Reading data from a file and manipulating it

Postby Yoriz » Sat Jun 14, 2014 10:10 pm

Are you wanting someone to write this simple solution for you ?
Using csv module is more simple then implimenting your own way of reading the csv.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 832
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Reading data from a file and manipulating it

Postby snippsat » Sun Jun 15, 2014 12:21 pm

I just want to keep it simple and not want to use the csv module. I am just looking for a simple solution.

csv module is simple,it would never make code more complicated.
It's no problem to write it without cvs module,you should really try to write something yourself.
Only one time,next time post some damn code :|
Code: Select all
def average(lst):
    return sum(lst) / float(len(lst))

age = []
height = []
with open('names.txt') as f:
    for row in f:
        row = row.split(', ')
        age.append(int(row[1]))
        height.append(int(row[2]))

print 'Average age: {:>8}\n\
Average height: {:>6}\n\
Shortest person: {:>3}\n\
Tallest person: {:>4}'\
.format(average(age), average(height), min(height), max(height))

Output:
Code: Select all
Average age:     29.4
Average height:  178.2
Shortest person: 170
Tallest person:  189
User avatar
snippsat
 
Posts: 217
Joined: Thu Feb 21, 2013 12:04 am


Return to General Coding Help

Who is online

Users browsing this forum: Baldyr, DrakeMagi, pete1845 and 5 guests