Skip to content
Advertisement

How to remove quote (‘) from the my scraped output

How can i get rid of the quotes in the output. I have tried to used .strip('') but the output does not seem to change.

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen('https://en.wikipedia.org/wiki/NIFTY_50')
soup = BeautifulSoup(html,'lxml')
niftylist_raw = soup.find('table', {'class': 'wikitable sortable'})

nifty_symbol =[]

for row in niftylist_raw.findAll('tr')[1:]:
    nifty_symbols = row.findAll('td')[1].text


    nifty_symbol.append(nifty_symbols.strip(''))
print(nifty_symbol)

The current output looks like this:

[‘ADANIPORTS.NSn’, ‘ASIANPAINT.NS’, ‘AXISBANK.NS’, ‘BAJAJ-AUTO.NS’, ‘BAJFINANCE.NS’, ‘BAJAJFINSV.NS’, ‘BHARTIARTL.NS’, ‘INFRATEL.NS’, ‘BPCL.NS’, ‘CIPLA.NS’, ‘COALINDIA.NS’, ‘DRREDDY.NS’, ‘EICHERMOT.NS’, ‘GAIL.NS’, ‘GRASIM.NS’, ‘HCLTECH.NS’, ‘HDFC.NS’, ‘HDFCBANK.NS’, ‘HEROMOTOCO.NS’, ‘HINDALCO.NS’, ‘HINDUNILVR.NS’, ‘BRITANNIA.NS’, ‘ICICIBANK.NS’, ‘INDUSINDBK.NS’, ‘INFY.NS’, ‘IOC.NS’, ‘ITC.NS’, ‘JSWSTEEL.NS’, ‘KOTAKBANK.NS’, ‘LT.NS’, ‘M&M.NS’, ‘MARUTI.NS’, ‘NESTLEIND.NS’, ‘NTPC.NS’, ‘ONGC.NS’, ‘POWERGRID.NS’, ‘RELIANCE.NS’, ‘SBIN.NS’, ‘SUNPHARMA.NS’, ‘TCS.NS’, ‘TATAMOTORS.NS’, ‘TATASTEEL.NS’, ‘TECHM.NS’, ‘TITAN.NS’, ‘ULTRACEMCO.NS’, ‘UPL.NS’, ‘VEDL.NS’, ‘WIPRO.NS’, ‘YESBANK.NS’, ‘ZEEL.NS’]

I would like to remove the upper quotes. This is because the exported file also has the quote.

I can just convert this to df = pd.Dataframe(nifty_symbol) which would solve the problem but I still want to learn how I could have used the .strip function. Any help would be helpful. Thanks

Advertisement

Answer

There are no actual quotes in the list of strings. The single quotes surrounding each element are put there by python to let you know it is a string, as opposed to some other datatype. If you print each element individually, or store it in a file, you will not see the quotes.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement