Search code examples
python-3.xunicodeunicode-string

Python - Convert unicode entity into unicode symbol


My request.json(), When I loop through the dict it returns from an API, returns "v\u00F6lk" (without the quotes)

But I want "völk" (without the quotes), which is how it is raw in the API.

How do I convert?

request = requests.post(get_sites_url, headers=api_header, params=search_sites_params, timeout=http_timeout_seconds)
return_search_results = request.json()
for site_object in return_search_results['data']:
    site_name = str(site_object['name'])
    site_name_fixed=str(site_name.encode("utf-8").decode())
    print("fixed site_name: " + site_name_fixed)

Solution

  • My Guess, the API is actually returning the literal version, so he is really getting:

    "v\\u00F6lk"
    

    Printing that gives what we think we are getting from the api:

    print("v\\u00F6lk")
    v\u00F6lk
    

    I am not sure if there is a better way to do this, but encoding it with "utf-8", then using "unicode_escape" to decode seemed to work:

    >>> print(bytes("v\\u00F6lk", "utf-8").decode("unicode_escape"))
    völk
    >>> print("v\\u00F6lk".encode("utf-8").decode("unicode_escape"))
    völk