python从文件中读取数据

时间:2015-03-21 20:59:40

标签: python

我有一个包含以下内容的文件:

        weight  height(ft)
John    240 5.3
Adam    300 6.1
Tom 140 6.9
Bob 135 4.6
Dan 170 5.5
Kang    190 5.9
Tong    167 4.1
Seth    120 6.7
Zoe 110 4.9
Watt    190 5.1

我想:

  1. 为所有人找到BMI
  2. 找到BMI最高的人,输出人名
  3. 找到BMI最低的人,输出人名。
  4. 寻找体重不足的人。
  5. 寻找超重的人。
  6. 这是我到目前为止所做的:

    import math
    
    def makeMagnitudeList():
        quakefile = open("Book1.txt","r")
        bmilist = [ ]
        namelist = [ ]
        for aline in quakefile:
            vlist = aline.split()
            namelist.append(vlist[0])
            namelist.append(((float(vlist[1])) / (pow(float(vlist[2])*12,2)))*703)  
            bmilist.append(namelist)           
        quakefile.close()
        return bmilist
    
    mag = makeMagnitudeList()
    
    maxmag = max(mag)
    maxIdx = mag.index(maxmag)
    
    minmag = min(mag)
    minIdx = mag.index(minmag)
    
    print(maxmag)
    print(maxIdx)
    
    print(minmag)
    print(minIdx)
    

2 个答案:

答案 0 :(得分:1)

Pandas非常适合数据管理,用numpy取代数学。使用pandas DataFrame对象如下......

import pandas as pd
import numpy as np

df = pd.read_csv(path + "Book1.txt")


def makeMagnitudeList(df = df):
    bmilist = list()
    namelist = list()
    weight = np.float64(df['weight'])
    height = (np.float64(df['height(ft)'])*12)**2
#    print(weight,height)
    bmi = (weight / height) * 703
#    print(weight[0], height[0])
#    bmi = (weight[0] / height[0]) * 703
#    print(bmi)
    return pd.Series(bmi,dtype=np.float64,name='bmi')

bmi_column = makeMagnitudeList(df)
df_bmi = pd.concat([df.ix[:,:],bmi_column],axis=1)
print(df_bmi)
print('\n')
min_bmi = df_bmi.ix[np.argmin(df_bmi['bmi']),:]
max_bmi = df_bmi.ix[np.argmax(df_bmi['bmi']),:]
print('Minimum bmi:')
print(min_bmi)
print('\n')
print('Maximum bmi:')
print(max_bmi)

返回以下输出:

   name  weight  height(ft)        bmi
0  John     240         5.3  41.711166
1  Adam     300         6.1  39.359939
2   Tom     140         6.9  14.355644
3   Bob     135         4.6  31.146621
4   Dan     170         5.5  27.435721
5  Kang     190         5.9  26.646637
6  Tong     167         4.1  48.499983
7  Seth     120         6.7  13.050420
8   Zoe     110         4.9  22.366259
9  Watt     190         5.1  35.662032


Minimum bmi:
name              Seth
weight             120
height(ft)         6.7
bmi           13.05042
Name: 7, dtype: object


Maximum bmi:
name              Tong
weight             167
height(ft)         4.1
bmi           48.49998
Name: 6, dtype: object

答案 1 :(得分:0)

现在,在第9行和第10行中,您将一个人的姓名,然后是他们的BMI追加到namelist。所以现在你有了

namelist = ['John', 41.71116648866739]

之后,您将namelist追加到bmilist,然后给您

namelist = ['John', 41.71116648866739]
bmilist = [['John', 41.71116648866739]]

现在重复这个过程:

namelist = ['John', 41.71116648866739, 'Adam', 39.35993908447551]

现在当您将namelist追加到bmilist时,您只需将引用附加到namelist对象,而不仅仅是其当前< EM>状态。这意味着,无论您将namelist追加到bmilist,您都始终引用namelist对象的当前值:

bmilist = [['John', 41.71116648866739, 'Adam', 39.35993908447551], [['John', 41.71116648866739, 'Adam', 39.35993908447551]]

注意你现在有两个对namelist对象的引用?

您可以像这样检查此行为:

>>> a = [1,2,3]
>>> b = []
>>> b.append(a)
>>> a.append(4)
>>> b.append(a)
>>> b
[[1, 2, 3, 4], [1, 2, 3, 4]]

那么你如何解决它?

好吧,您可以在每次迭代开始时将namelist重新分配一个空列表:

def makeMagnitudeList():
    ...
    for aline in quakefile:
        namelist = []
        ...

或者,你可以改用词典:

def makeMagnitudeList():
    quakefile = open("Book1.txt","r")
    bmidict = {}
    for aline in quakefile:
        vlist = aline.split()
        bmidict[vlist[0]] = float(vlist[1]) / pow(float(vlist[2])*12,2)))*703
    quakefile.close()
    return bmilist

P.S。您使用的是哪个版本的Python?如果是3.x,那么您可以取消float()次调用,因为/不会截断结果。如果是2.x,请在文件顶部使用from __future__ import division(如果有的话,请使用hashbang / shebang之后)以获得相同的行为:

from __future__ import division

import math

def makeMagnitudeList():
    ...
    for aline in quakefile:
        ...
        bmidict[vlist[0]] = vlist[1] / (703 * (12 * vlist[2]) ** 2)

实际上,您甚至可以进一步简化:

        bmidict[vlist[0]] = vlist[1] / (101232 * vlist[2] ** 2)