###質問
下のコードを行うとエラーがでるので解決したい
実現したいこと
test.txtのファイルの中身の数値から
0から0.1
0.1から0.2
0.2から0.3
0.3から0.4
0.4から0.5
0.5から0.6
0.6から0.7
0.7から0.8
0.8から0.9
0.9から1.0
の範囲のそれぞれの数値と個数を出力したいです。
発生している問題・エラーメッセージ
f+=1 TypeError: unsupported operand type(s) for +=: '_io.TextIOWrapper' and 'int'
該当のソースコード
python
1 path='/home/api_pack/youzitu_value-a_1/youzitu-0704' 2 result=[] 3 a=0 4 b=0 5 c=0 6 d=0 7 e=0 8 f=0 9 g=0 10 h=0 11 i=0 12 j=0 13 k=0 14 with open(path+".txt", mode='r', encoding='UTF-8') as f: 15 data=f.readlines() 16 for i in data: 17 result.append(i.replace("\n", "")) 18 #print(result) 19 #float_list=(map(float, result)) 20 #print(list(result)) 21 #print(type(result)) 22 23 for s in result: 24 try: 25 print(float(s)) 26 if '0'<s<'0.1': 27 print(s) 28 a+=0 29 elif '0.1'<s<'0.2': 30 print(s) 31 b+=1 32 elif '0.2'<s<'0.3': 33 print(s) 34 c+=1 35 elif '0.5'<s<'0.4': 36 print(s) 37 d+=1 38 elif '0.4' < s < '0.5': 39 print(s) 40 e+=1 41 elif '0.5'<s<'0.6': 42 print(s) 43 f+=1 44 elif '0.6'<s<'0.7': 45 print(s) 46 g+=1 47 elif '0.7'<s<'0.8': 48 print(s) 49 h+=1 50 elif '0.8'<s<'0.9': 51 print(s) 52 i+=1 53 elif '0.9'<s<'1.0': 54 print(s) 55 j+=1 56 57 except ValueError: 58 print("error") 59 60 61 62 print(" 0 and 0.1:"+str(a)) 63 print("0.1 and 0.2:"+str(b)) 64 print("0.2 and 0.3:"+str(c)) 65 print("0.3 and 0.4:"+str(d)) 66 print("0.4 and 0.5:"+str(e)) 67 print("0.5 and 0.6:"+str(f)) 68 print("0.6 and 0.7:"+str(g)) 69 print("0.7 and 0.8:"+str(h)) 70 print("0.8 and 0.9:"+str(i)) 71 print("0.9 and 1.0:"+str(j))
.txtの中身
0.755229
0.688727
0.660857
0.725014
0.609122
0.667595
0.664538
0.64256
0.647082
0.591497
0.55077
0.612957
0.83207
0.67802
0.701618
0.908475
0.659222
0.534892
0.661645
0.552581
0.790303
0.727885
0.69056
0.579366
0.681916
0.441917
0.626703
0.790303
0.685454
0.796727
0.728756
0.492767
0.789395
0.720935
0.582183
0.568498
0.580289
0.725347
0.740118
0.663689
0.694524
0.448931
0.764399
0.71526
0.487369
0.717317
0.600473
0.615127
0.60676
0.574313
0.378411
0.370908
0.73546
0.636587
0.847458
0.412695
0.643728
0.681713
0.5
0.593561
試したこと
.txtは文字列扱いだったので、末尾の\nを削除。
文字列を浮動小数点に変換すること
多分まだできていないかも
#のところは試行錯誤の跡です。
こちらの質問が他のユーザーから「やってほしいことだけを記載した丸投げの質問」という指摘を受けました。
回答2件
良いと思った回答にはグッドを送りましょう。
グッドが多くついた回答ほどページの上位に表示されるので、他の人が素晴らしい回答を見つけやすくなります。
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/11/15 16:34
2022/11/15 16:37 編集
2022/11/15 16:43
2022/11/15 16:52
2022/11/15 16:58
2022/11/15 21:24
2022/11/16 00:33
2022/11/16 00:43