簡體   English   中英

Python 3.6帶有Unicode字符和字節的亂碼字符串

[英]Python 3.6 Messy String with Unicode characters and Bytes

所以我使用NewsPlease從Common Crawl新聞報道中獲取文章標題,但是當我拿到文章標題時,它們是正常編碼字符和Unicode字節的混合,我無法正確編碼。 選擇其中一個標題:

x = articles[800].title

如果我在spyder中調用x,它將返回:

'Las 10 canciones m\\xc3\\xa1s populares de la semana'

當我使用print(x)我得到:

Las 10 canciones m\xc3\xa1s populares de la semana

但如果嘗試使用以下方法正確編碼:(如其他帖子所示)

x.encode('latin1').decode('utf8')

它回來了

'Las 10 canciones m\\xc3\\xa1s populares de la semana'

這顯然是不正確的。

有人有什么建議嗎? 我順便使用Python 3.6

找到了解決方案:

x = 'this is a test of the Spanish word m\\xc3\\xa1s'
x = x.encode('latin1').decode('unicode_escape').encode('latin1').decode('utf8')
print(x)
'this is a test of the Spanish word más'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM