2017-06-04 15 views
0

我搜索了一段時間,但找不到有用的東西。 我嘗試刪除Paython列表中的字符串中的這些十六進制代碼,但無法弄清楚如何做到這一點。他們是字符串格式! 除非從我的數據(特魯姆普Twitter的獲取):Python字符串刪除像'總統王牌\ xe2 \ x80 \ x99s'十六進制代碼

tweets[7] 
'rt @ lindseygrahamsc : i support president trump\\xe2\\x80\\x99s desire to reenter the paris accord after the agreement becomes a better deal for america\\xe2\\x80\\xa6' 

謝謝!

+0

你想刪除的十六進制代碼,或者你詢問他們是否可以轉換到ASCII? (請注意,這些都是大於128的數字,因此不是標準ASCII集的一部分。) – THK

+0

很難爲您提供幫助,因爲您沒有提供有關如何獲取此字符串的詳細信息。順便說一句,也許你應該回到你的問題的根源,正確的編碼tweets,例如, status.text.encode(「utf-8」) – tagoma

+0

謝謝!我只是想刪除它們並專注於純文本,所以在這個方向上的一些解決方案將非常棒! – Squall

回答

0

試試這個方式,它只能工作於子串「\\x**」:

import re 
tweets = 'rt @ lindseygrahamsc : i support president trump\\xe2\\x80\\x99s desire to reenter the paris accord after the agreement becomes a better deal for america\\xe2\\x80\\xa6' 
re.sub(r'(\\x(.){2})', '',tweets) 

輸出:

'rt @ lindseygrahamsc : i support president trumps desire to reenter the paris accord after the agreement becomes a better deal for america' 
相關問題