質問編集履歴

1

1行ならばできるようになりましたが複数行でやろうとするとエラーができます

2017/09/02 07:07

投稿

NaritatsuSaito
NaritatsuSaito

スコア9

test CHANGED
@@ -1 +1 @@
1
- n個のデータの代表値を求める
1
+ csvファイルからn個のデータの代表値を求めるー複数行でのやり方 python3
test CHANGED
@@ -22,28 +22,80 @@
22
22
 
23
23
 
24
24
 
25
- 初心者かわかりせん
25
+ 1行ならばきるよになったのですが複数行で行おうとするとエラーがで
26
26
 
27
27
 
28
28
 
29
29
  エラーメッセージ
30
30
 
31
+
32
+
33
+ ValueError Traceback (most recent call last)
34
+
35
+ <ipython-input-7-6ed550d9bc09> in <module>()
36
+
37
+ 6 columns = row.rstrip().split(',')
38
+
39
+ 7 for column in columns:
40
+
41
+ ----> 8 n=float(column)
42
+
31
- ```
43
+ 9 list.append(n)
44
+
45
+ 10 repeat = len(list)//20
46
+
47
+
48
+
49
+ ValueError: could not convert string to float:
32
50
 
33
51
 
34
52
 
35
53
  ###該当のソースコード
36
54
 
55
+
56
+
37
- かけないです。
57
+ list = []
58
+
59
+ new_list=[]
60
+
61
+ new_list_2=[]
62
+
63
+ with open ('test5.csv', encoding ='utf-8') as f:
64
+
65
+ for row in f:
66
+
67
+ columns = row.rstrip().split(',')
68
+
69
+ for column in columns:
70
+
71
+ n=float(column)
72
+
73
+ list.append(n)
74
+
75
+ repeat = len(list)//20
76
+
77
+ for t in range (20):
78
+
79
+ for num in list[t*repeat:t*repeat+repeat]:
80
+
81
+ new_list.append(num)
82
+
83
+ new_value=(sum(new_list)/len(new_list))
84
+
85
+ new_list_2.append(new_value)
86
+
87
+ new_list=[]
88
+
89
+ print (new_list_2)
38
90
 
39
91
 
40
92
 
41
93
  ###試したこと
42
94
 
43
- ウェブ調べてまがわかりません。
95
+ コードも素人丸出し申し訳ないで
44
96
 
45
97
 
46
98
 
47
99
  ###補足情報(言語/FW/ツール等のバージョンなど)
48
100
 
49
- 複数行ある場合はさらにどようにしたら良いでしょうか?
101
+ 複数行ある場合はなぜエラーが出るのでしょうか?