读书人

python n=str.index(#039;=#039;)ValueErr

发布时间: 2012-03-21 13:33:15 作者: rapoo

python n=str.index('=')ValueError: substring not found
有一个字符串 id2=9787111289234,通过检索等号后面的字符串链接网址。
我是这样写的:str = fopen.readline()
n=str.index('=')
str=str[n+1:]
能够运行,但是运行结束时出现错误提示:Traceback (most recent call last):
File "D:\Reader\python2.5\ISBNimage\china_pubImage\ggetImagefromchina_pub.py", line 55, in <module>
n=str.index('=')
ValueError: substring not found

单独在python shell中能够使用n=str.index('=');str=str[n+1:]这两个语句,但是为什么一写到程序中就出错呢?

分不多,请大家见谅!!


[解决办法]
确定读取到的行有等号么。?
你可能需要进行异常处理

话说我觉得可以这样写
line=fopen.readline()
url=line.split("=")[0]

[解决办法]

探讨

引用:

url=line.split("=")[0]

这一句能读取等号后面的字符吗

读书人网 >perl python

热点推荐