简体   繁体   中英

removing characters from list elements in python

I am extracting content with the help of scrapy into an array. Each element has the unwanted characters ": " inside which I would like to remove as efficient as possible.

v = response.xpath('//div[@id="tab"]/text()').extract()
>>> v
['Marke:', 'Modell:']
>>> for i in v : re.sub(r'[^\w]', '', i)
... 
'Marke'
'Modell'

Now that seems to work, but how can I retain the result? In my code, v hasn't changed:

>>> v
['Marke:', 'Modell:']

You can solve this with a list comprehension :

>>> v = response.xpath('//div[@id="tab"]/text()').extract()
>>>
>>> import re
>>> v = [re.sub(r'[^\w]', '', i) for i in v]
>>> v
['Marke', 'Modell']

I think that pulling in regex for this is a little overkill: use the string replace method:

v = ['Marke:', 'Modell:']
v = [str.replace(':', '') for str in v]
print(v)

Output:

['Marke', 'Modell']

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