當(dāng)我將句子復(fù)制并粘貼How brave they’ll all think me at home!到 Mac 上的空白 TextEdit rtf 文檔中時,它看起來很好。但是,如果我以編程方式創(chuàng)建一個明顯相同的 rtf 文件,并將相同的句子寫入其中,在打開 TextEdit 時,它會顯示為How brave theya€?ll all think me at home! 在以下代碼output中,沒問題,但是在 TextEdit 中查看文件時,右單引號 (這里用作撇號),unicode U-2019。header = r"""{\rtf1\ansi\ansicpg1252\cocoartf1671\cocoasubrtf400{\fonttbl\f0\fswiss\fcharset0 Helvetica;}{\colortbl;\red255\green255\blue255;}{\*\expandedcolortbl;;}\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0\f0\fs24 \cf0 """sen = 'How brave they’ll all think me at home!'with open('staging.rtf', 'w+’) as f: f.write(header) f.write(sen) f.write('}')with open('staging.rtf') as f: output = f.read()print(output)我從https://www.i18nqa.com/debug/utf8-debug.html發(fā)現(xiàn)這可能是由“UTF-8 字節(jié)被解釋為 Windows-1252”引起的,這似乎是有道理ansicpg1252的標題表示美國 Windows。但是我仍然不知道如何解決它,即使在這里閱讀了類似的問題:Encoding of rtf file。我試過用ansi無效替換mac。并且添加,encoding='utf8'到 open 函數(shù)似乎也無濟于事。(順便說一句,使用 rtf 的原因是能夠?qū)С鰩в蓄伾幋a的單詞的句子,允許它們被手動編輯,然后讀回以進行進一步處理)。
- 1 回答
- 0 關(guān)注
- 407 瀏覽
添加回答
舉報
0/150
提交
取消