in python i need to transform particular string where some character need to be transformed in unicode sintax. For example:
"Città"->"Citt\u00E0"
I need a string in the second form in order to print it on a file.
I've tried some function but the problem i have now is that the string must be transform in the following way
"Citt\xe0"->"Citt\u00E0"
there is a way to perform this transformationa and the reverse operation?
CodePudding user response:
I can understand that you want to print \u00EO in a file literally.
Here is a sample program that, looks if ord of character is >127, and if so converts it to the format you wanted.
In this example sample.txt will have output as. Hope this heps
Cittàá
Citt\u00E0\u00E1
with open("sample.txt","w") as fd:
my_var='Citt\xe0\xe1'
final_str = [my_var[idx] if ord(my_var[idx]) <= 127 else "\\u00" str(hex(ord(my_var[idx]))).replace("0x","").capitalize() for idx in range(len(my_var))]
val="".join(final_str)
fd.write(my_var "\n")
fd.write(val)
CodePudding user response:
The following (partially commented) script could help:
import re
x = "\\x66