Nora
Nora

Reputation: 15

Parsing and processing tabular .txt file

I'm supposed to write a program that reads a file and then calculates the max and min and the average of the height and weight columns and adds an additional columnn that has the BMI calculated based on each height and weight. The file is formatted like so:

Name        Height(m)   Weight(kg)
Joe         1.82        72.57
Mary        1.60        63.50
Dion        1.90        90.71
Kayla       1.72        66.31
Jose        1.78        70.23
Sofia       1.63        65.12
Erik        1.98        92.21
Sara        1.57        65.77

The output should be as follows: expected output

I have the very very basics down (reading the text file). I know what I need to do next which is iterate through each column and add up all the data to get the average, etc etc. But I don't know how to code for it. You don't have to write the whole code for me because I know I don't have much, but if someone could just point me in the right direction/give me a hint as to how to proceed it'd be very helpful.

data_file = open("data.txt", "r")

total = 0 
for line_str in data_file:  
    line_str = line_str.strip()
    total+=1 

Upvotes: 1

Views: 182

Answers (3)

Oussama Karoui
Oussama Karoui

Reputation: 33

Your problem can be solved by this way:

data_file = open("data.txt", "r")`

total = 0

for line_str in data_file:      
         [float(s) for s in line_str.split() if s.isdigit()]

Upvotes: 0

tuomastik
tuomastik

Reputation: 4906

With Pandas library the solution is simple:

import pandas as pd

df = pd.read_csv("data.txt", delim_whitespace=True)
df['BMI'] = df['Weight(kg)'] / (df['Height(m)']**2)
df.describe()

Upvotes: 1

coderanger
coderanger

Reputation: 54211

The method you need is str's split(). The takes a string, like a line in a file, and breaks it up in to multiple chunks. After that you'll also need to use float() to parse the floating point number from the string chunk.

Upvotes: 2

Related Questions