开发者

'QString' object has no attribute 'strip'?

So I'm trying to read a web page using mechanize.Browser() module in Python. The problem is that br.open(url) does not work, because python returns the error 开发者_C百科in the title.

Here's the code:

url = "http://www.myserver.com/prda.php?validate=" + licensey
readurl = br.open(url).read()

At the latter line, I get:

File "/usr/lib/python2.7/urllib.py", line 1038, in unwrap
url = url.strip()
AttributeError: 'QString' object has no attribute 'strip'

I tried using unicode(readurl), unicode (br.open(url).read()), readlines() instead of read(), str (in place of unicode)... I either get the same error, or None output from br.open.read()

Help?


I guess you are developing a PyQt application and 'licensey' is a input you are taking from some 'QTextEdit' element.

In your application the 'url' is of type 'QString'. And there is no 'strip' method in 'QString' data type. Since open() method expects you to send a parameter of type 'str', you just need to typecast the variable 'url'.

Just add the line

url = str(url)

before calling the method open(url). Hope this helps :)


It is really strange that PyQt QString does not includes a strip() method, but it has a trimmed() method which does the same. See here: http://pyqt.sourceforge.net/Docs/PyQt4/qstring.html#trimmed. What are really lacking in PyQt are the variants lstrip() and rtrip().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜