0
   The string that could not be encoded/decoded was: 熔树脂温度(℃)

   'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

I am trying to encode the ascii character but I am getting error. I have tried with

熔树脂温度(℃).encode('utf-8')
熔树脂温度(℃).encode('utf8')
unicode(熔树脂温度(℃),'utf-8')

Nothing is working.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tarun Khaneja
  • 451
  • 11
  • 23
  • I believe you need unicode not ascii – Andria Apr 06 '17 at 05:48
  • `.encode('utf-8')` didn't work? It works for me. – xrisk Apr 06 '17 at 05:50
  • 1
    I'm not sure if python-2.7 has unicode enabled though, you'll probably need python3 – Andria Apr 06 '17 at 05:52
  • 1
    Can you show the actual code? Devil is in the details. Python 3 is much better at unicode than python 2. Is there some reason why you are using this old technology when its been mostly fixed in the new stuff? – tdelaney Apr 06 '17 at 05:52
  • Hello? That isn't python code. It can't produce the error you say. Can you post an example, maybe a few lines from the python shell? I'm going to take a guess, but really you should make this easier for us. – tdelaney Apr 06 '17 at 06:04

1 Answers1

0

To convert into unicode:

In [1]: str1 = '熔树脂温度(℃)'

In [2]: print str1
熔树脂温度(℃)

In [3]: str1
Out[3]: '\xe7\x86\x94\xe6\xa0\x91\xe8\x84\x82\xe6\xb8\xa9\xe5\xba\xa6(\xe2\x84\x83)'

In [4]: unicode_str1 = str1.decode('UTF-8')

In [5]: print unicode_str1
熔树脂温度(℃)

In [6]: unicode_str1
Out[6]: u'\u7194\u6811\u8102\u6e29\u5ea6(\u2103)'
rkatkam
  • 2,634
  • 3
  • 18
  • 29
  • This assumes that OP is running a python shell that is using utf-8. Suppose this was a windows code page? – tdelaney Apr 06 '17 at 06:01