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

前端 未结 10 2671
夕颜
夕颜 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 01:54

    If you have a Unicode string, and you want to write this to a file, or other serialised form, you must first encode it into a particular representation that can be stored. There are several common Unicode encodings, such as UTF-16 (uses two bytes for most Unicode characters) or UTF-8 (1-4 bytes / codepoint depending on the character), etc. To convert that string into a particular encoding, you can use:

    >>> s= u'£10'
    >>> s.encode('utf8')
    '\xc2\x9c10'
    >>> s.encode('utf16')
    '\xff\xfe\x9c\x001\x000\x00'
    

    This raw string of bytes can be written to a file. However, note that when reading it back, you must know what encoding it is in and decode it using that same encoding.

    When writing to files, you can get rid of this manual encode/decode process by using the codecs module. So, to open a file that encodes all Unicode strings into UTF-8, use:

    import codecs
    f = codecs.open('path/to/file.txt','w','utf8')
    f.write(my_unicode_string)  # Stored on disk as UTF-8
    

    Do note that anything else that is using these files must understand what encoding the file is in if they want to read them. If you are the only one doing the reading/writing this isn't a problem, otherwise make sure that you write in a form understandable by whatever else uses the files.

    In Python 3, this form of file access is the default, and the built-in open function will take an encoding parameter and always translate to/from Unicode strings (the default string object in Python 3) for files opened in text mode.

    0 讨论(0)
  • 2020-11-22 01:57

    Here is an example code

    import unicodedata    
    raw_text = u"here $%6757 dfgdfg"
    convert_text = unicodedata.normalize('NFKD', raw_text).encode('ascii','ignore')
    
    0 讨论(0)
  • 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'<text>' 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.)

    0 讨论(0)
  • 2020-11-22 02:04

    You can use encode to ASCII if you don't need to translate the non-ASCII characters:

    >>> a=u"aaaàçççñññ"
    >>> type(a)
    <type 'unicode'>
    >>> a.encode('ascii','ignore')
    'aaa'
    >>> a.encode('ascii','replace')
    'aaa???????'
    >>>
    
    0 讨论(0)
  • 2020-11-22 02:05
    >>> text=u'abcd'
    >>> str(text)
    'abcd'
    

    If the string only contains ascii characters.

    0 讨论(0)
  • 2020-11-22 02:09

    See unicodedata.normalize

    title = u"Klüft skräms inför på fédéral électoral große"
    import unicodedata
    unicodedata.normalize('NFKD', title).encode('ascii', 'ignore')
    'Kluft skrams infor pa federal electoral groe'
    
    0 讨论(0)
提交回复
热议问题