2013-10-20 87 views
-1

我有一個文件,其後是幾個數字(例如,名稱10 20 30)。 我需要從每一行中提取數字,並使用它們來計算這些數字的平均值,然後重新打印名稱,然後逐行平均。如何從行中提取數字並在Python中進行計算?如何在Python中使用ReadLine()從文件中提取整數?

+0

使用正則表達式,並請問一個問題,並張貼你嘗試了什麼 – pylover

回答

0

如果你有一個看起來像這樣的文件:

list 
list 
list 
list 
90 
43 
54 
67 
12 
45 

可以使用isdigit()功能提取數字,然後每個數字加在一起在一起,算多少號發現然後被劃分總和它找到了多少個數字。

info = file("info.txt").read() 
info = info.split("\n") 

avarage=0 
count=0 
for item in info: 
    if item.isdigit(): 
     count=count+1 
     avarage=avarage+int(item) 

print avarage/count 

結果是51

相關問題