0

When I insert the device data into MySQL(v5.5.6) using python(v3.2). It encountered a problem.

This is device A (It contains three unicode and a blank space): '\u202d\u202d \u202d'

And device B (It is only a blank space): ' '

The problem is when i insert all device data into MySQL , Error is

Duplicate entry 'activate_device-20151201-1-5740-01000P---‭‭ ‭--' for key 'PRIMARY'

I guess MySQL has deal the '\u202d'(A unicode to reverse string maybe?).

How can I simulate the process in python3 like MySQL? How can I avoid the duplicate?

The expected result is translate '\u202d\u202d \u202d' to ' ' in python3.

Please help me.

1 Answers1

0

There are some ambiguities here. Do you want to keep only the visible ascii characters or also visible unicode characters ?

If you want to keep only visible ascii characters, the simple way is to use the python inbuilt string module.

import string
new_string = "".join(filter(lambda x:x in string.printable, original_string))

For your specific usecase, a space is part of visible ascii - so the above will convert '\u202d\u202d \u202d' and ' ' to ' '

AbdealiLoKo
  • 3,261
  • 2
  • 20
  • 36
  • That's a bit more complicated. Here is a good explanation on how to do that using regex http://stackoverflow.com/questions/92438/stripping-non-printable-characters-from-a-string-in-python – AbdealiLoKo Jan 06 '16 at 08:27