0

need help with replacing words(solved)

i got text with format b'*' where * is random word i need to remove b' ' and left only word (problem i cant remove b without touching other b in words like blob) https://code.sololearn.com/cLTXEA7BUi9x/?ref=app

16th Jun 2021, 2:43 AM
Marat Bahtyarov
Marat Bahtyarov - avatar
7 Réponses
+ 2
so, you could do: q = [s[2:-1] for s in b] ;P
16th Jun 2021, 3:26 AM
visph
visph - avatar
+ 1
# with regular expression, it's quite easy: import re p = re.compile("b'([^']*)'") r = lambda m: m.group(1) b = ['b\'blob\'','b\'cloud\''] q = [re.sub(p,r,s) for s in b] print(*q)
16th Jun 2021, 2:53 AM
visph
visph - avatar
+ 1
i just do that b=['b\'blob\'','b\'cloud\''] for i in b: i=i[1:] i=i.replace('\'','') print(i)
16th Jun 2021, 2:55 AM
Marat Bahtyarov
Marat Bahtyarov - avatar
+ 1
my solution works also for all b'*' formats in a string with anything before or after, such as: b = ['start b\'blob\' other b\'cloud\' end'] ;)
16th Jun 2021, 2:59 AM
visph
visph - avatar
0
i just find solution by myself but still thanks
16th Jun 2021, 2:54 AM
Marat Bahtyarov
Marat Bahtyarov - avatar
0
thanks but my code only output list of words
16th Jun 2021, 3:17 AM
Marat Bahtyarov
Marat Bahtyarov - avatar
0
👌 actually yes,thank you!
16th Jun 2021, 4:26 AM
Marat Bahtyarov
Marat Bahtyarov - avatar