Replacing "<" with "*" with a Python regex -


i need go through strings in list "listb", replacing character "<" "*".

i tried this:

import re in listb:     = re.sub('\<','\*', 0) 

but keep getting typeerror: expected string or buffer.

not sure doing wrong , examples on net not help.

see docs

as per seth's comment, best way using regular expressions going be:

listb = [re.sub(r'<',r'*', i) in listb] 

as @paco, said, should using str.replace() instead. if still want use re:

you're putting 0 string supposed go! typeerror third parameter. it's int, needs string.

side note: use raw strings, denoted r'', in regexes, don't have escape.

>>> listb = ['abc', '<asd*', '<<>>**'] >>> in listb: ...     = re.sub(r'<',r'*', i) ...     print ...  abc *asd* **>>** >>> listb ['abc', '<asd*', '<<>>**'] 

if want new list replaced, do:

>>> listx = [] >>> in listb: ...     listx.append(re.sub(r'<',r'*', i)) ...  >>> listx ['abc', '*asd*', '**>>**'] >>> listb ['abc', '<asd*', '<<>>**'] >>> listb = listx 

if don't want create new list, can iterate through indices.

note you're not changing i in list. create new list here. each i here own variable, doesn't point listb.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -