次のような文字列がある場所を読み取るいくつかのtxtファイルがあります。
「はい!イワシの缶詰め! \uD83E\uDD23」
問題はそれです:私がやっているとき
"Yes! Sardines in a can distancing! \uD83E\uDD23".encode('utf-16','surrogatepass' ).decode('utf-16)
python は \UDD23 または \UD83E を個別に 2 つの単一文字と見なすため、Unicode ポイントは絵文字に変換されます。
出力:
Yes! Sardines in a can distancing!
また、上記の文字列の長さを len() 関数を使って見ると、出力は 37 です。
ただし、テキスト ファイルから同じ文字列を読み取る場合、Python は \UDD23 または \UD83E を個別の文字として読み取ります。つまり、合計 12 文字です。結果。つまり、Unicode ポイントは絵文字に変換されません。以下のコードを使用しました。
count=0
for item in enumerate(list(tweet_dict)):
if item[0]==75:
a=item[1]['text']
print('Length of the string is: ',len(str(a)))
print(a.encode('utf-16', 'surrogatepass').decode('utf-16'))
出力は次のとおりです。
Length of the string is: 47
Yes! Sardines in a can distancing! \uD83E\uDD23