简体   繁体   中英

Removing Quotation Marks from string in a list

Given a list :

q = ['"hello','it's','me']

I want to remove "'s" and the '"' at the beginning of hello to obtain:

q = ['hello','it','me']

I wrote:

for i in ['"s','"']:
    q = [w.replace(i,'') for w in q]

This results in q=['"hello",'it','me'] . The code does not seem to work for the quotation marks.

(I cannot comment because I don't have enought points so please excuse me for posting as an answer).

First your initial list q is not valid, the 'it's element will throw invalid syntax.

The easiest will be to sanitize your list elements before they're pushed, is this possible for you? For example, in the case of double quotes use

element = '"hello'
element.replace('"', '')

In the case of the single quote and the s:

element = "it's"
single_index = element.find("'")
new_element = element[0:single_index]

I hope this helps

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