Answer the question
In order to leave comments, you need to log in
How to remove line breaks in unicode?
Standard methods do not delete completely.
In short, the situation is that I take a certain document from the elasticsearch library, with a certain structure, which I need to redo. Those. these strings are already in Elastic and I expect that there will be no need to fiddle with them, but for a strange, even incomprehensible reason, the standard library does not escape quotes, instead of fields in quotes, arrays in JSON are returned with apostrophes, i.e. For example:
{
"lastname": "Иванов",
"education": [
'пту №1',
'университет патрисы лумумбы'
],
"hobbies": "Люблю вышивать "крестиком" и
вязать на спицах"
}
def filter(mystr) -> str:
mystr = str(mystr)
mystr = mystr.replace('\r\n', u' ')
mystr = mystr.replace('\r', u' ')
mystr = mystr.replace('\n', u' ')
mystr = mystr.replace('\\r', u' ')
mystr = mystr.replace('\\n', u' ')
mystr = mystr.replace('\\r\\n', u' ')
mystr = mystr.replace('\'', u'"')
mystr = mystr.replace('\"', u'\\\"')
mystr = mystr.replace(u'True', u'true')
mystr = mystr.replace(u'False', u'false')
mystr = mystr.replace(u'None', u'null')
re.sub('^\s+|\n|\r|\s+$', u'', mystr)
return str(mystr)
Answer the question
In order to leave comments, you need to log in
Регуляркой:
import re
mystr = " balabla\n zzz "
re.sub("^\s+|\n|\r|\s+$", '', mystr)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question