Home > Back-end >  Python: add special char at specific place in strings
Python: add special char at specific place in strings

Time:10-05

I have a dataframe with a string column 'text'.

This column correspond to a reading (with Python) of docx files and after reading I did processing for Machine Learning.

Before processing I had for example for one row: 'This is a sentence by <<\xa0me\xa0>>' After processing, for Machine Learning I have: 'This is a sentence by << me >>'

After Machine Learning applied I want to add '\xa0' after << and before >> for all rows where << and >> appears in order to have 'This is a sentence by <<\xa0me\xa0>>'

How can I do it ? I thought with regex but not sure.

CodePudding user response:

To spell out what a_guest suggests in the comments:

s = 'This is a sentence by << me >>'
s = s.replace('<< me >>', '<<\xa0me\xa0>>')

Note however that '\xa0' is en escape sequence, and so this will be interpreted as one character. If you need the literal text, use the r (raw) prefix:

s = 'This is a sentence by << me >>'
s = s.replace('<< me >>', r'<<\xa0me\xa0>>')

(And if you're unsure, try both and check)

  • Related