python读取rtf文件_在python中将unicode文本输出到RTF文件

我试图从

python脚本输出unicode文本到RTF文件.对于背景,

Wikipedia说

For a Unicode escape the control word \u is used, followed by a 16-bit signed decimal integer giving the Unicode UTF-16 code unit number. For the benefit of programs without Unicode support, this must be followed by the nearest representation of this character in the specified code page. For example, \u1576? would give the Arabic letter bāʼ ب, specifying that older programs which do not have Unicode support should render it as a question mark instead.

但是,我无法弄清楚如何从Python中将unicode代码点输出为“带有Unicode UTF-16代码单元号的16位带符号十进制整数”.我试过这个:

for char in unicode_string:

print '\\' + 'u' + ord(char) + '?',

但是当在文字处理器中打开时,输出只会呈现为乱码;问题似乎是它不是UTF-16代码.但不知道如何得到它;虽然可以用utf-16编码,但是如何获得代码?

顺便说一下,PyRTF不支持unicode(它被列为“todo”),虽然pyrtf-NG应该这样做,但该项目似乎没有维护并且文档很少,所以我很谨慎地使用它 – 生产系统.

编辑:我的错误.上面的代码中有两个错误 – 正如Wobble所指出的那样,字符串必须是一个unicode字符串,而不是已编码的字符串,而上面的代码产生一个结果,字符之间有空格.正确的代码是这样的:

convertstring=""

for char in unicode(,'utf-8'):

convertstring = convertstring + '\\' + 'u' + str(ord(char)) + '?'

这很好,至少对于OpenOffice.我将此作为其他人的参考

(以下讨论后进一步纠正了一个错误).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值