2016-05-03 33 views
0

のそれは時に仕事をしました私は別のスクリプトでそれをテストしました...

初心者の助けが必要です。私は私が自分で解決することができなかった私のpythonスクリプトに問題があります。スクリプトは、テキストファイルから浮動小数点を読み込み、計算を実行することになっています。番号R[]に番号を取得できません。

以下は、最初の行(s[0])が公称値で、2行目(s[1])が公差であり、次の行がいくつかの抵抗です。


3300.0

10.0

3132.0

3348.5

3557.3

3467.4

3212.0

3084.6

3324.0

私は、次のコードを持っている:車輪の再発明する必要はありません

R = [] 
Ntoolow = Nlow = Nhigh = Ntoohigh = 0.0 
lines = 0 

def find_mean(q): 
    tot = 0.0 
    c = len(q) 
    for x in range (c): 
     tot += q[x] 
     return tot/c 


def find_median(q): 
    c = len(q) 
    if c%2: 
     return float(q[int(c/2)]) 
    else: 
     c /= 2 
     return (q[int(c)]+q[int(c-1)])/2.0 


file_name = input("Please enter the file name: ") 
file = open(file_name, "r") 

s = file.readlines() 

Rnom = float(s[0]) 
Tol = float(s[1]) 


keepgoing = True 

while keepgoing: 
    s = file.readline() 
    if s == "": 
     keepgoing = False 
    else: 
     R.append(float(s)) 


lines = len(file.readlines()) 
N = lines - 2 
R.sort() 


Rmin = R[0] 
Rmax = R[N-1] 

Rlowerlimit = Rnom - Tol 
Rupperlimit = Rnom + Tol 


for rn in R: 
    if rn < Rlowerlimit: 
     Ntoolow += 1 
    elif rn < Rnom: 
     Nlow += 1 
    elif rn <= Rupperlimit: 
     Nhigh += 1 
    else: 
     Ntoohigh += 1 


Ptoolow = 100.0 * Ntoolow/N 
Plow = 100.0 * Nlow/N 
Phigh = 100.0 * Nhigh/N 
Ptoohigh = 100.0 * Ntoohigh/N 


Rmean = find_mean(R) 
Rmedian = find_median(R) 


print("Total number of parts tested: " + str(N)) 
print("The largest resistor is: " + str(Rmax) + " and the smallest is: " + str(Rmin)) 
print("The mean is: " + str(Rmean) + " and the median is: " + str(Rmedian)) 
print("The percentage of resistors that have too low tolerance is: " + str(Ptoolow) + "%") 
print("The percentage of resistors that have low tolerance is: " + str(Plow) + "%") 
print("The percentage of resistors that have high tolerance is: " + str(Phigh) + "%") 
print("The percentage of resistors that have too high tolerance is: " + str(Ptoohigh) + "%") 

file.close() 
+0

あなたの質問を得るカント... uがしたいですかリストR []に戻り数字を追加しますか? –

+0

何が問題だと思われますか?あなたはエラーを受け取りますか?もしそうなら、どちらですか? – usr2564301

答えて

0

、と

while keepgoing: 
    s = file.readline() 
    if s == "": 
     keepgoing = False 
    else: 
     R.append(float(s)) 

for i in s: 
    R.append(float(i)) 

その寄付をあなたのコードを置き換え私は答えを...ここで

は、私はあなたのコードを編集した後、

Total number of parts tested: -2 
The largest resistor is: 3348.5 and the smallest is: 10.0 
The mean is: 1.11111111111 and the median is: 3300.0 
The percentage of resistors that have too low tolerance is: -200.0% 
The percentage of resistors that have low tolerance is: -0.0% 
The percentage of resistors that have high tolerance is: -50.0% 
The percentage of resistors that have too high tolerance is: -200.0% 
+0

最初の2つの数字(3300.0と10.0)をリストに追加しないようにするにはどうしたらいいですか? – 557365726e616d65

+0

最初の2つの数字を追加したくないですか? (3300.0と10.0)? –

+0

最初の2桁(3300.0と10.0)を追加しないようにするには、このように2番目の要素からテキストファイルを開始する必要があります.S [2:]の場合は –

0

、Pythonのは、それがファイルの終わりに達していますとき、あなたはしないでください知っていますあなた自身の句を追加する必要があります(プラス、空行で終わる可能性がありますか? "あまり確かではない)。

このコードを試してみてください。

with open(file_name) as f: 
    try: 
     R = float(f.readlines()) 
    except TypeError: 
     pass 
+0

'TypeError例外:float型()の引数は、Fとしてオープンして '(FILE_NAME)を次のようにこれらをキャッチ文字列またはnumber' – SparkAndShine

+0

^なければなりません: 試し: R =フロート(f.readlines()) TypeError例外を除き、 : pass' –

+0

ありがとう、変わった – dahui

1

Pythonは、このようなCSV、numpyのやパンダなどのCSVファイルから情報を読み取ることができるライブラリの数を持っています。次のことを試してみてください。

import csv 
with open('filename.csv','r') as f: 
    reader = csv.reader(f) 

あなたは、このような行を反復処理することができます。

for row in reader: 
    print(row) 
1

は空行をスキップし、ラインがrstrip()\nを壊す削除出力されます。

filename = 'test.dat' 

with open(filename, 'r') as f: 
    s = [float(line.rstrip()) for line in f if line.rstrip()] 

print(s) 
#[3300.0, 10.0, 3132.0, 3348.5, 3557.3, 3467.4, 3212.0, 3084.6, 3324.0] 
関連する問題