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

前端 未结 10 2677
夕颜
夕颜 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:13

    There is a library that can help with Unicode issues called ftfy. Has made my life easier.

    Example 1

    import ftfy
    print(ftfy.fix_text('ünicode'))
    
    output -->
    ünicode
    

    Example 2 - UTF-8

    import ftfy
    print(ftfy.fix_text('\xe2\x80\xa2'))
    
    output -->
    •
    

    Example 3 - Unicode code point

    import ftfy
    print(ftfy.fix_text(u'\u2026'))
    
    output -->
    …
    

    https://ftfy.readthedocs.io/en/latest/

    pip install ftfy

    https://pypi.org/project/ftfy/

提交回复
热议问题