简体   繁体   中英

How to remove square brackets and '' from string?

from bs4 import BeautifulSoup
import requests
import pandas as pd
import time

headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'}

r = requests.get('https://www.hltv.org/stats/teams/7969/Nemiga?startDate=2019-01-01&endDate=2019-12-31')
soup = BeautifulSoup(r.text,'html.parser')
results = soup.find_all('div',{'class':'columns'})
results2 = soup.find_all('div',{'class':'large-strong'})

first_round = []
for result in results2:
    KDratio = result.contents

Output is ['1.04'].. is there a way I can get just 1.04 as the output?

Thanks

.contents is a list containing all children. That's not what you want. You want the simple text content of the tag, for which you should use .string .

Assuming your output is out = "['1.04']" you have (at least) two options.

  1. String Operation
out = "['1.04']"
num = float(out.strip("[']"))  # 1.04
  1. String Parsing
import ast

out = "['1.04']"
num = float(ast.literal_eval(out)[0])  # 1.04

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