Convert a Unicode string to a string in Python (containing extra symbols)

前端 未结 10 2852
夕颜
夕颜 2020-11-22 01:20

How do you convert a Unicode string (containing extra characters like £ $, etc.) into a Python string?

10条回答
  •  滥情空心
    2020-11-22 02:00

    Well, if you're willing/ready to switch to Python 3 (which you may not be due to the backwards incompatibility with some Python 2 code), you don't have to do any converting; all text in Python 3 is represented with Unicode strings, which also means that there's no more usage of the u'' syntax. You also have what are, in effect, strings of bytes, which are used to represent data (which may be an encoded string).

    http://docs.python.org/3.1/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit

    (Of course, if you're currently using Python 3, then the problem is likely something to do with how you're attempting to save the text to a file.)

提交回复
热议问题