简体   繁体   中英

Python2 - How to remove characters from output?

I made this script that outputs the balance of a group of addresses. The output however comes in a list of one. How do I get python to extract the value from the list, so it doesn't show the ['']?

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)

Regards.

Try this:

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

print balance

When you have a list foo , foo[0] gets the first element of foo . Since your list has only one element, that's the only element of foo . Then you can just print it out. (Similarly, you could use foo[1] to get the second element, foo[2] to get the third, etc.)

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

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

In [3]: ''.join(balance)
Out[3]: '0.00622801 BTC'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM