Issue
I have a dollar rate parser, but when displaying the name of the currency, spaces are also displayed before its name, how to remove them?
def usd():
r = requests.get('https://fx-rate.net/USD/')
if r.status_code == 200:
soup = bs4(r.text, "html.parser")
tbody = soup.find_all("tbody")[1]
for info in tbody:
try:
name = info.find("td").text
rate = info.find("a", class_ = "1rate").text
except:
pass
print(f"{name} | {rate}")
else:
print(r.status_code)
usd()
Example output:
Danish Krone | 7.65 Egyptian Pound | 19.7 Ethereum | 0.0008 Euro | 1.03 Hong Kong Dollar | 7.85
Solution
Try:
print(f"{name.strip()} | {rate}")
strip() will remove leading and trailling whitespaces.
Refer to this guide for deeper explanation: https://www.digitalocean.com/community/tutorials/python-trim-string-rstrip-lstrip-strip
Answered By - birmsi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.