Python2 - 如何从输出中删除字符?

时间:2016-08-23 02:34:00

标签: python json python-2.7

我制作了这个脚本,输出了一组地址的余额。但是输出列在一个列表中。如何让python从列表中提取值,所以它没有显示['']?

from lxml import html
import requests

page = requests.get('https://blockchain.info/xpub/xpub6BfKpqjTwvH21wJGWEfxLppb8sU7C6FJge2kWb9315oP4ZVqCXG29cdUtkyu7YQhHyfA5nt63nzcNZHYmqXYHDxYo8mm1Xq1dAC7YtodwUR')
tree = html.fromstring(page.content)

balance = tree.xpath('//*[@id="final_balance"]/font/span/text()')

print str(balance)

问候。

2 个答案:

答案 0 :(得分:1)

试试这个:

balance = tree.xpath('//*[@id="final_balance"]/font/span/text()')[0]

print balance

如果您有一个列表foo,则foo[0]会获得foo的第一个元素。由于您的列表只有一个元素,因此foo元素。然后你可以打印出来。 (同样,您可以使用foo[1]获取第二个元素,foo[2]获取第三个元素等等。)

答案 1 :(得分:0)

In [1]: balance
Out[1]: ['0.00622801 BTC']

In [2]: type(balance)
Out[2]: list

In [3]: ''.join(balance)
Out[3]: '0.00622801 BTC'
相关问题