[英]Write unicode gif to file in python
我有一個 Unicode 格式的 GIF 文件(或任何圖像格式):
>>> data
u'GIF89a,\x000\x00\ufffd\ufffd\x00\x00\x00\x00\ufffd\ufffd\ufff...
我想將此寫入文件:
>>> f = open('file.gif', 'wb')
>>> f.write(data)
但我收到一個錯誤:
UnicodeEncodeError at /image
'ascii' codec can't encode characters in position 10-11: ordinal not in range(128)
我該怎么做呢?
您必須將字符串顯式編碼為 unicode
f.write(data.encode('utf-8'))
嘗試這個:
utf8data = data.encode('UTF-8')
open('file.gif', 'w').write(utf8data)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.