str在csv python中浮动错误 [英] str to float error in csv python
问题描述
输出= []
打开(undercurve.csv,'rU')为f5:
读者= csv.reader(F5,分隔符= '')
reader1 =拉链(*读取器)
。在reader1行:
值为在= 0
对于i行:
value = float(i)
output.append(i)
然而,当我运行它时,我得到一个
ValueError
Traceback(最近最后一次调用)
174 else:
175 filename = fname
- > 176 exec exec(scriptname,filename,'exec')in glob,loc
177 else:
178 def execfile(fname,* where):
C:\ Users \Robert\Downloads\May.py in< module>()
142 value = 0
143 for i in row:
- > 144 value = float(i)
145 output.append(i)
146
ValueError:无法将字符串转换为浮点数。
我猜这是前一部分:
I = 0
相开放( undercurve.csv, 'W')为F3:
对于i在undercurve_1:
平均=
方差= 0.2
分数= undercurve(1000)[:10]
对于点数:
x = str(item)
f3.write (x +,)
f3.write(\\\
)
如果我尝试使用x = float(item),我得到一个错误:
TypeError
Traceback最后调用)
174 else:
175 filename = fname
- > 176 exec exec(scriptname,filename,'exec')in glob,loc
177 else:
178 def execfile(fname,* where):
C:\ Users \Robert\Downloads\May.py in< module>()
130中的物品点数:
131 x = float(item)
- > 132 f3.write(x +,)
133 f3.write(\ n)
134
TypeError:不支持的操作数类型为+:'float'和'STR'。
我不完全确定在这种情况下该做什么。
0.485863651248,0.0387115424974,0.287431660408,0.368734594828,0.618990463984,0.112220965205,0.418700402941,0.193754757929,0.573411295973,-0.192370410069,
- 1.42282833703,-1.52808081061,-1.03071829996,-1.00330662742,-1.23896275168,-1.09742340137,-0.940839402591,-0.918657969034,-1.37832945051,-0.932452513278,
'p>这些只是前2行undercurve.csv文件
显然,结束的文件的每一行以逗号结尾。这使得一个(空字符串)出现。 float('')会给你ValueError。
I'm trying to open a csv file that is 10 columns x 100 rows and I first want to swap it to 100 columns x 10 rows, which I believe I used the zip function properly for, and then make 10 arrays with a 100 floats in each.
output = []
with open("undercurve.csv",'rU') as f5:
reader = csv.reader(f5, delimiter= ',')
reader1 = zip(*reader)
for row in reader1:
value = 0
for i in row:
value = float(i)
output.append(i)
However, when I run it I get a
ValueError
Traceback (most recent call last)
174 else:
175 filename = fname
--> 176 exec compile(scripttext, filename, 'exec') in glob, loc
177 else:
178 def execfile(fname, *where):
C:\Users\Robert\Downloads\May.py in <module>()
142 value = 0
143 for i in row:
--> 144 value = float(i)
145 output.append(i)
146
ValueError: could not convert string to float.
I'm guessing this is from the previous part:
i = 0
with open("undercurve.csv",'w') as f3:
for i in undercurve_1:
mean = i
variance = .2
points = undercurve(1000)[:10]
for item in points:
x = str(item)
f3.write(x + ",")
f3.write("\n")
If I try to use x = float(item), I get an error:
TypeError
Traceback (most recent call last)
174 else:
175 filename = fname
--> 176 exec compile(scripttext, filename, 'exec') in glob, loc
177 else:
178 def execfile(fname, *where):
C:\Users\Robert\Downloads\May.py in <module>()
130 for item in points:
131 x = float(item)
--> 132 f3.write(x + ",")
133 f3.write("\n")
134
TypeError: unsupported operand type(s) for +: 'float' and 'str'.
I'm not exactly sure as to what to do in this situation.
0.485863651248,0.0387115424974,0.287431660408,0.368734594828,0.618990463984,0.112220965205,0.418700402941,0.193754757929,0.573411295973,-0.192370410069,
-1.42282833703,-1.52808081061,-1.03071829996,-1.00330662742,-1.23896275168,-1.09742340137,-0.940839402591,-0.918657969034,-1.37832945051,-0.932452513278,
These are just the first 2 lines of the undercurve.csv file
Apparently the end of each line of your file ends with a comma. This makes a '' (empty string) appear. And float('') gives you the ValueError.
这篇关于str在csv python中浮动错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!