Python Typecast";340“;在';34';失去最后一个零
场景1:Python Typecast";340“;在';34';失去最后一个零,python,regex,Python,Regex,场景1: 输入:“1-0:1.7.0(00.471*kW)” 正则表达式:“[0-9]-[0-9]:1.7.0\([0]{1,}(.*\*kW\)” 输出:471(应如此) 场景2: 输入:“1-0:1.7.0(00.470*kW)” 正则表达式:“[0-9]-[0-9]:1.7.0\([0]{1,}(.*\*kW\)” 输出:47而不是470 操作:output=int(str(str(float({0:.4f}).格式(float(re.search([0-9]-[0-9]):1.7.0\(
输入:
“1-0:1.7.0(00.471*kW)”
正则表达式:
“[0-9]-[0-9]:1.7.0\([0]{1,}(.*\*kW\)”
输出:471(应如此) 场景2:
输入:
“1-0:1.7.0(00.470*kW)”
正则表达式:
“[0-9]-[0-9]:1.7.0\([0]{1,}(.*\*kW\)”
输出:47而不是470 操作:
output=int(str(str(float({0:.4f}).格式(float(re.search([0-9]-[0-9]):1.7.0\([0]{1,}(.*)\*kW\)”,linestr).组(1);).replace(“,”).replace(“,”).replace(“*,”))
问题:
当输入与场景2类似时,我希望输出为470而不是47
如何获取包括尾随零在内的所有字符?您只需要
后面的数字:
s = "1-0:1.7.0(00.471*kW)"
print(int(re.findall(":1.7.0\([0]+\.(\d+)\\*kW\)",s)[0]))
471
s = "1-0:1.7.0(00.470*kW)"
print(int(re.findall(":1.7.0\([0]+\.(\d+)\\*kW\)",s)[0]))
470
或者简单地说:
print(int(re.findall("\([0]+\.(\d+)\\*kW\)",s)[0]))
(int(re.findall(“:1.7.0([0]+\.(\d+\*kW)”,linestr)[0]))索引器:列表索引超出范围使用您提供的输入工作,它实际上是什么样子的?您没有完全复制我提供的代码。你没有逃过第一关<代码>\([0]
我使用了你的正则表达式,我得到:=(int(re.findall(:1.7.0([0]+\.(\d+\*kW)”,linestr)[0]))索引器:列表索引超出了intput 1-0:1.7.0(00.471*kW)的范围你仍然没有使用我提供的正则表达式