-1

#When I try to transform in my code the letters from hexadecimal code to Spanish accent didn´t work.

#My IDLE is Spider

Table

[Transform to Spanish letters] (https://i.stack.imgur.com/rGcZQ.png)

#Example: #Definition three examples of a txt or str with #Spanish chr:

line3='RODR\xCDGUEZ' #->str
line4='Vel\xE1squez' #->str
line5='Andr\xE9s C\xe1ceres' #->str

--------------------------------

#Please could solve the problem, because the string is the same but the idle work in two different ways, aren't similar results.

Case 1 Correct

line2='RODR\xCDGUEZ'

#Result in the Variable explorer 'RODRÍGUEZ'

Case 2 Incorrect

#(with modify de str internal by code)

line='RODR=CDGUEZ' 

#with "=" to will replace in the next command

line=line.replace('=','\\x')

#The result is without Í??

'RODR\xCDGUEZ'

#I don't understand why the idle now don't recognize "Í" and maintain "\xCD"

#th

Julio
  • 3
  • 2
  • 1
    Make a [mcve]. Describe what "didn't work". Please don't paste links to images of text. Put the *actual text* in the question so it is readable and copyable. – Mark Tolonen Feb 27 '23 at 18:18
  • 1
    Note that `\x` is *case-sensitive* when entering a string literal. – Mark Tolonen Feb 27 '23 at 18:20
  • for example, this code work ok: this assignation of str it's ok the idle transform correctly. myStr1='RODR\xCDGUEZ Vel\xE1squez L\xF3pez_Palma_Pablo_Marcelo' – Julio Feb 27 '23 at 19:05
  • Fine, but what doesn't work OK? Again, `\x` is *case-sensitive*. Please [edit your question](https://stackoverflow.com/posts/75584113/edit) instead of putting new information or code in comments. – Mark Tolonen Feb 27 '23 at 19:08
  • However whe I try to work with this code in the idle isn't work: line='=?Q?RODR\CDGUEZ?' – Julio Feb 27 '23 at 19:10
  • Thanks @markTolonen !! I proved with \x and \X and the result is different with \x its correct. My problem is about the kind of input of "str" for one side work and for another side didn't work. Define the str[ line2='RODR\xCDGUEZ'] and run the idle the result in the "explorer variable" and it's result its correct [RODRÍGUEZ]. However, if I insert [line='RODR=CDGUEZ'] and with code I change the "=" for "\x" with replace operator [line.replace('=','\\x')]. In the "explorer variable" appear [line='RODR\xCDGUEZ'] and didn´t translate to this word [RODRÍGUEZ]. Sorry if I don't express correctly – – Julio Feb 28 '23 at 15:51

1 Answers1

0

You cannot generate an escape code by simply building a string that looks like one. Additional steps are needed to convert the string to a bytes object and then the manually constructed escape codes can be decoded:

>>> s = 'RODR=CDGUEZ'
>>> s.replace('=','\\x').encode('ascii').decode('unicode-escape')
'RODRÍGUEZ'

I think what you really have is an (incomplete) email header, as that uses the =hh notation for encoding non-ASCII data bytes. The missing information is what encoding the bytes represent. Below shows a valid encoded email header:

>>> import email.header
>>> for value, encoding in email.header.decode_header('=?iso-8859-1?q?RODR=CDGUEZ?='):
...    print(value.decode(encoding))
...
RODRÍGUEZ
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251