python中的字符串操作

Dou*_*oug 3 python

我有一个像这样的python文件

import urllib2

try:
    data = urllib2.urlopen('http:....').read()
except urllib2.HTTPError, e:
    print "HTTP error: %d" % e.code
except urllib2.URLError, e:
    print "Network error: %s" % e.reason.args[1]

print data1
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样

>>>
15.95

>>>
Run Code Online (Sandbox Code Playgroud)

我需要对data1(或任何替代变量)执行一些操作,这样当我打印data1(或新变量)时,输出没有额外的行.换句话说,我希望它看起来像这样:

>>>
15.95
>>>
Run Code Online (Sandbox Code Playgroud)

任何帮助都会很棒我对python相对较新.我一直在用\ r进行调整,似乎无法让它工作.

Gin*_*kas 7

也许您只需要从变量中删除空格?使用data1.strip().阅读 str.strip()以获取更多信息.

顺便说一句,要明确地看到空格,请使用print repr(data1).