Sie werden sehen, was geschieht, wenn Sie häufiger
drucken
Sie eine Liste von Listen
so ist ein [x] selbst erstellt haben Liste
wenn Sie stringify die Liste seiner '[9000]
so yo Sie können keinen Float daraus machen, weil es keine Nummer ist
Sie müssten die Klammern abziehen; oder nicht eine Liste von Listen erstellen mit
mit Ihrem Beitrag als Eingabe beginnen:
import re
handle = '''
Python - ValueError: could not convert string to float: [9000]
Ask Question
up vote
0
down vote
favorite
I am trying to understand this error message (Python 2.7). I see there are
others who have asked this question previously, but I do not understand the
explanations given there so I am asking again. Here is my code. Yes, I am a
newbie trying to learn the basics so please keep that in mind when you answer.
There's a reason I haven't been able to understand previous posts.
'''
y = list()
print y
for line in handle:
line = line.rstrip()
if re.findall('[0-9]+', line) != [] :
y.append(re.findall('[0-9]+', line))
print y
a = [map(int, b) for b in y]
print a
for x in range(len(a)):
if len(a[x]) == 1:
b=str(a[x])
print b
c=float(b)
kehrt:
[]
[['9'], ['0'], ['0'], ['0'], ['0'], ['2'], ['7']]
[[9], [0], [0], [0], [0], [2], [7]]
[9]
Traceback (most recent call last):
File "test4.py", line 31, in <module>
c=float(b)
ValueError: could not convert string to float: [9]
Ich bin nicht sicher, was Ihr Endziel ist, aber wenn Sie dies tat:
b=str(a[x][0])
print b
c=float(b)
es funktionieren würde und das Rück
9
0
0
0
0
2
7
Können Sie einige Links zu den vorherigen Posts bereitstellen? – Tankobot
Bitte gehen Sie durch die Hoffnung, dass Sie Ihre Antworten finden können: http://stackoverflow.com/questions/8420143/valueerror-could-not-convert-string-to-float-id – manoj
Es sieht aus wie Ihr Versuch, die Zeichenfolge ' 9000] in einen Float, der nicht funktioniert, weil er zusätzliche Klammern hat. Sie können das entweder in der Datei korrigieren, die Sie gerade lesen, oder Sie können die Klammern entfernen, indem Sie sie ausschneiden: '' "[9000]" [1: -1] == "9000" ''. – Tankobot