0

In the following code, raw and want are both 1, but they are different in computers. I want to convert raw to want. I have a lot of such strs, how can I convert them in python?

raw = '1'  
want = '1'  
print(ord(raw))  # 65297
print(ord(want)) # 49
max yue
  • 375
  • 2
  • 4
  • 10
  • `ord(raw.strip())` ? – Klaus D. Jul 15 '17 at 11:11
  • 3
    Possible duplicate of [Convert unicode representation of number to ascii string](https://stackoverflow.com/questions/25313773/convert-unicode-representation-of-number-to-ascii-string) – Aran-Fey Jul 15 '17 at 11:16
  • The following url solve my problem. Thank you. http://www.cnblogs.com/kaituorensheng/p/3554571.html – max yue Jul 15 '17 at 11:33
  • Possible duplicate of [Python: Convert Unicode to ASCII without errors](https://stackoverflow.com/questions/2365411/python-convert-unicode-to-ascii-without-errors) – psychOle Jul 15 '17 at 13:58

1 Answers1

0

raw looks like a unicode '1' (maybe U+FF11 ?) rather than an ascii '1'. You could use a dictionary that has the unicode characters as keys and the ascii characters as values. That might look something like

lookup = {'1': '1', ...}

And then, when you want to convert the character, you could do something like

want = lookup[raw]

edit: This might be a more robust answer.

RagingRoosevelt
  • 2,046
  • 19
  • 34