问题描述
我正在使用re.findall()从文件中的行提取数字,我可以很好地获取数字,但是该函数添加了引号,双引号和方括号,因此我无法转换字符串浮。如何删除数字中的[['']“字符以进行转换?
这是我的代码:
import re
count = 0
total = list()
hand = open('mBox-short.txt')
for line in hand:
line = line.rstrip()
x = re.findall('New Revision: ([0-9.]+)',line)
if len(x) > 0:
count += 1
a = str(x)
total.append(a)
print(total) # test print
total1 = list(map(float,total)) # line 24 -- where I get the ValueError
print(sum(total1)/count)
输出:
["['39772']","['39771']","['39770']","['39769']","['39766']","['39765']","['39764']","
['39763']","['39762']","['39761']","['39760']","['39759']","['39758']","['39757']","
['39756']","['39755']","['39754']","['39753']","['39752']","['39751']","['39750']","
['39749']","['39746']","['39745']","['39744']","['39743']","['39742']"]
Traceback (most recent call last):
File "revisions.py",line 27,in <module>
total1 = list(map(float,total))
ValueError: Could not convert string to float: ['39772']
我正在尝试将数字转换为浮点数,以便可以计算平均值。 我想念什么?在哪里可以找到有关操纵输出格式的信息,以便可以使用它?
谢谢!
解决方法
s = "['123']"
s = s[2:-2] # remove first 2 and last 2 characters
print(float(s))
# 123.0
只需从字符串中删除前两个字符。
,使用列表推导将total
中的所有值转换为浮点数。
total = [float(i.split("'")[1]) for i in total]