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

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

    No answere worked for my case, where I had a string variable containing unicode chars, and no encode-decode explained here did the work.

    If I do in a Terminal

    echo "no me llama mucho la atenci\u00f3n"
    

    or

    python3
    >>> print("no me llama mucho la atenci\u00f3n")
    

    The output is correct:

    output: no me llama mucho la atención
    

    But working with scripts loading this string variable didn't work.

    This is what worked on my case, in case helps anybody:

    string_to_convert = "no me llama mucho la atenci\u00f3n"
    print(json.dumps(json.loads(r'"%s"' % string_to_convert), ensure_ascii=False))
    output: no me llama mucho la atención
    

提交回复
热议问题