[Python] Attribute Error:'list' object has no attribute'replace'

While studying Python scraping, when I was processing values, I got ʻAttribute Error:'list' object has no attribute'replace'`, so I will leave a countermeasure in the memo

Cause of the error

I was trying to write logic to delete a specific string in a list obtained by Python. But the error => ʻAttributeError:'list' object has no attribute'replace'`

>>> str_list = ['aaabbb','article']
>>> str_list_new = str_list.replace('a', '')
AttributeError: 'list' object has no attribute 'replace'

Countermeasures

It seems that you should convert the array to a single character string, replace it with replace (), and then return it to the array.

>>> str_list = ['aaabbb','article']
>>> str_list_new = [item.replace('a', '') for item in str_list]
>>> str_list_new
['bbb', 'rticle']

reference https://spcx8.hatenablog.com/entry/2017/07/05/204423

Recommended Posts

[Python] Attribute Error:'list' object has no attribute'replace'
AttributeError:'NoneType' object has no attribute'loader'
[Python] Countermeasures for "AttributeError:'NoneType' object has no attribute'group'" of googletrans
That Python code has no classes ...
[Dlib] Attribute Error: module'dlib' has no attribute ...
Blender has no object selection order
With xmpppy'_ssl._SSLSocket' object has no attribute'issuer'
[Wandb] Attribute Error: module'wandb' has no attribute'init'
Dealing with Python error "Attribute Error: module'scipy.misc' has no attribute'imresize'" in deep learning
Python built-in object
When'DataFrame' object has no attribute'ix' appears in pandas
How to deal with module'tensorflow' has no attribute'〇〇'
Python built-in object
AttributeError: module ‘torch.utils’ has no attribute ‘data’ solution
Python attribute alias
Solution when module'XXX' has no attribute'XXX' in Python
Django "Query Set, Object has no attribute'foo'" solution
Use numpy without stress! (Interpretation of error "Attribute Error:'float' object has no attribute'sin'" (action?))
AttributeError: I was addicted to'module' object has no attribute'MyTestCase'
Object oriented in python
About Python3 ... (Ellipsis object)