如何在python中解码javascript unicode字符串?

时间:2018-04-11 08:15:05

标签: python unicode utf ucs2

我发现它是TWO HEARTS here的字符。我试图解码它:

a = '\ud83d\udc95'
a.encode('utf-8').decode('utf-8')
UnicodeEncodeError: 'utf-8' codec can't encode characters in position 0-1: surrogates not allowed

如何获得正确的符号?

谢谢。

2 个答案:

答案 0 :(得分:2)

您链接的页面会告诉您

  

Python Escape u' \ U0001f495'

使用它:

>>> u'\U0001f495'
''

答案 1 :(得分:1)

使用json模块帮助您解决不同的2018-04-18 16:53:48.325 [DEBUG] (Task worker for ':') com.crashlytics - Invoked Crashlytics Developer Tools with arguments: -androidOutputManifestDirectories /Users/<my_dir>/work/<project_root>/<main_project>/build/intermediates/manifests/full/<valiant_dir> -injectableManifest true -androidBaseManifest /Users/<my_dir>/work/<project_root>/<main_project>/src/main/AndroidManifest.xml -buildEvent true -androidManifest /Users/<my_dir>/work/<project_root>/<main_project>/src/main/AndroidManifest.xml -apiSecret <XXXXXXXXXXXXXXXXXXXXXXXXX> -generateResourceFile true -tool io.fabric.tools.gradle -version 1.25.3 -properties /Users/<my_dir>/work/<project_root>/<main_project>/fabric.properties -buildId <XXXXXXXXXXXXXXXXXXXXXXXXX> -targetResValueDir /Users/<my_dir>/work/<project_root>/<main_project>/build/generated/fabric/res/<valiant_dir> -projectPath /Users/<my_dir>/work/<project_root>/<main_project> -androidRes /Users/<my_dir>/work/<project_root>/<main_project>/src/main/res -androidAssets /Users/<my_dir>/work/<project_root>/<main_project>/build/intermediates/assets/<valiant_dir> -googleServicesResDir /Users/<my_dir>/work/<project_root>/<main_project>/build/generated/res/google-services/<valiant_dir> 2018-04-18 16:53:48.327 [DEBUG] (Task worker for ':') com.crashlytics - Manifest and properties apiKey is empty. Looking for google_app_id 2018-04-18 16:53:48.328 [DEBUG] (Task worker for ':') com.crashlytics - Google-services res dir: /Users/<my_dir>/work/<project_root>/<main_project>/build/generated/res/google-services/<valiant_dir>/values/values.xml 2018-04-18 16:53:48.328 [DEBUG] (Task worker for ':') com.crashlytics - Found google_app_id: <XXXXXXXXXXXXXXXXXXXXXXXXX> 2018-04-18 16:53:48.330 [DEBUG] (Task worker for ':') com.crashlytics - apiKey is <XXXXXXXXXXXXXXXXXXXXXXXXX> 2018-04-18 16:53:48.330 [DEBUG] (Task worker for ':') com.crashlytics - Generating crashlytics resources 2018-04-18 16:53:48.330 [DEBUG] (Task worker for ':') com.crashlytics - Generating build info into assets folder: /Users/<my_dir>/work/<project_root>/<main_project>/build/intermediates/assets/<valiant_dir> 2018-04-18 16:53:48.330 [DEBUG] (Task worker for ':') com.crashlytics - Updating build properties at: /Users/<my_dir>/work/<project_root>/<main_project>/build/intermediates/assets/<valiant_dir>/crashlytics-build.properties 2018-04-18 16:53:48.331 [DEBUG] (Task worker for ':') com.crashlytics - Set build id to <XXXXXXXXXXXXXXXXXXXXXXXXX> 2018-04-18 16:53:48.331 [DEBUG] (Build Event Notification) com.crashlytics - Build Event: <application_id> ID:<XXXXXXXXXXXXXXXXXXXXXXXXX> ApiKey:<XXXXXXXXXXXXXXXXXXXXXXXXX> Tool:io.fabric.tools.gradle 1.25.3 Base Package Name:<package_name> Twitter Client Application Id:null API Secret Null? false Extra Details:[null] 2018-04-18 16:53:48.337 [DEBUG] (Build Event Notification) com.crashlytics - REQUEST: https://api.crashlytics.com/spi/v1/platforms/android/apps/<application_id>/built 2018-04-18 16:53:49.654 [DEBUG] (Build Event Notification) com.crashlytics - POST response: [reqId=<XXXXXXXXXXXXXXXXXXXXXXXXX>] 200

Unicode