How do i remove the special chars that show as `\uxxx` in python3 string object?

merlin

python string object as follow:

The site of the old observatory in Bern \u200bis the point of origin of the CH1903 coordinate system at 46°57′08.66″N 7°26′22.50″E\ufeff / \ufeff46.9524056°N 7.4395833°E\ufeff / 46.9524056; 7.4395833.

I want to remove these chars \u200b \ufeff that show as raw unicode.

think-maths

Encode it to ascii and ignore errors

>>> s = 'The site of the old observatory in Bern \u200bis the point of origin of the CH1903 coordinate system at 46°57′08.66″N 7°26′22.50″E\ufeff / \ufeff46.9524056°N 7.4395833°E\ufeff / 46.9524056; 7.4395833'
>>> s.encode('ascii', 'ignore')
b'The site of the old observatory in Bern is the point of origin of the CH1903 coordinate system at 465708.66N 72622.50E / 46.9524056N 7.4395833E / 46.9524056; 7.4395833'

To replace unicode character with whitespace to keep the length same, you can use

#length of original string

>>> s = 'The site of the old observatory in Bern \u200bis the point of origin of the CH1903 coordinate system at 46°57′08.66″N 7°26′22.50″E\ufeff / \ufeff46.9524056°N 7.4395833°E\ufeff / 46.9524056; 7.4395833'
>>> len(s)
179

#to maintain the same length

>>> new_s = s.encode('ascii',errors='ignore').decode('utf-8')
>>> final_s = new_s + ' ' * (len(s) - len(new_s))
>>> final_s
'The site of the old observatory in Bern is the point of origin of the CH1903 coordinate system at 465708.66N 72622.50E / 46.9524056N 7.4395833E / 46.9524056; 7.4395833            '
>>> len(final_s)
179

this will add additional space at last to maintain the length

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I do it faster in java? Copy chars of a long string

How do I convert a string to a list of chars?

How do I convert a list of chars to a string in purescript

MySQL: How do I search and replace chars at the beginning of a string

How to remove special characters in a string in Python 3?

Remove Unicode code (\uxxx) in string Python

HOw to apply regex for converting a string to camel case and remove all special chars from the string-Javascript

How to assert a string having special chars in it?

How I can HTML Special Chars remove without image SRC in string?

How do I remove certain special characters from a string in Lua?

In Gradle how do I reference an imported ant task with special chars?

How do i remove chars from a string?

How do I convert special chars of passwords typed with CAPSLOCK?

How do I remove Chars from the end of a Char*?

How to remove special chars form string in hive

How do I remove 1 instance of x characters in a string and find the word it makes in Python3?

how to remove special chars like this █ from string in php

How do I json_decode string with special chars (" \\ " )

How do I concat a string of chars together from a matrix in python?

How do I convert Pandas object to a list in python3

how to replace this special chars from a string in Javascript

remove some special chars from string in javascript

How do I remove special character like "," within a string in a DataFrame?

How do I collect chars into a string in C?

How to remove duplicate chars in a string?

How do I ignore special chars and numbers from my input?

How do I interpret ASCII values of characters of a string as chars in C?

How do I remove some chars at the end of a string?

How Do I check if a string contains special characters or numbers in python