我试图使用find和string切片方法来提取行末的数字。但是我得到了这个不匹配的错误,因为我回到了第18位,但是根据我的阅读和研究,这个位置假设是18点我错过了什么?
str = ('X-DSPAM-Confidence:0.8475')
atpos = str.find(':')
print atpos
sppos = str.find(' ',atpos)
print sppos
host = float(str[atpos + 1:sppos])
print host
答案 0 :(得分:1)
str =('X-DSPAM-Confidence:0.8475') atpos = str.find(':')
sppos = str.find('',atpos)
host = float(str [atpos + 1:]) 打印(主机)
答案 1 :(得分:0)
您应该使用string.split
方法而不是查找:
分隔符的具体位置,
_str = 'X-DSPAM-Confidence:0.8475'
host = float(_str.split(':')[1])
print(host)
答案 2 :(得分:0)
str = ('X-DSPAM-Confidence:0.8475')
atpos = str.find(':')
sppos = str.find(' ',atpos)
host = float(str[atpos + 1:])
print host
答案 3 :(得分:0)
text = "X-DSPAM-Confidence: 0.8475";
spacePos = text.find(" ")
number = text[spacePos::1]
#not really necessary but since we are just learning and playing
strippedNumber = number.lstrip();
result = float(strippedNumber)
def reprint(printed):
print printed
reprint(result)