0

I am migrating a software product ,eventually I come to this problem.

s = '–' # https://www.fileformat.info/info/unicode/char/0096/index.htm 

in python2

s.encode('iso8859_15').decode('iso8859_15')  # u'-'
s.encode('utf-8').decode('iso8859_15')  # u'-'

in python3

s.encode('iso8859_15').decode('iso8859_15')
Traceback (most recent call last):
File "<stdin>", line 1, in <module> 
File "C:\Python37\lib\encodings\iso8859_15.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 0: character maps to <undefined>

s.encode('utf-8').decode('iso8859_15')  # 'â\x80\x93'

Please somebody explain , Why is that so and what is the solution for this.

Thanks in advance

I tried replacing this character with '-'(hyphen) but automation test cases failed, and modification of automation test cases is prohibited.

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • 2
    What is it that you are trying to achieve and why? Just randomly trying out decode and encode will not get you far. Note that `'–'` is https://www.fileformat.info/info/unicode/char/2013/index.htm, not the one you linked to. – luk2302 Jan 30 '23 at 12:54
  • Can't reproduce it (works in *Python 3*). [\[SO\]: Passing utf-16 string to a Windows function (@CristiFati's answer)](https://stackoverflow.com/a/59521873/4788546). In *Python 2* try: `s = u'–'`. – CristiFati Jan 30 '23 at 12:57

0 Answers0