Python 2.6 使用系统编码在 C-API 中写入字符串
我有一个 Python 2.6 项目,我想使用系统编码将 utf-8 消息写入 stdout。然而,这样的函数似乎直到Python 3.2才存在:
PySys_FormatStdout
http:// docs.python.org/dev/c-api/sys.html
有没有办法从 Python 2.6 做到这一点?
为了澄清,我有一个横幅需要在 Py_Initialize() 之后和主解释器运行之前打印。该字符串是一个 c 语言文字,包含: “\n 和版权 \xC2\xA9”,
其中 \xC2\xA9 是 utf-8 版权符号。我在 gdb 中验证了版权符号的编码正确。
更新: 我刚刚决定所有这些悲伤都是不必要的,我将从启动横幅中删除有问题的角色。这方面的问题太多,而且缺乏文档。我的期望是,这将像 Tcl 一样,其中:
- 嵌入式解释器的 C-API 将使在系统编码中以 unicode 形式轻松编写 stdout,而不是某些默认的 ascii 编码
- 如果存在,则不会引发异常当前编码中不存在违规字符。相反,会显示一些默认的替换字符。
- 不需要仅仅为了找出系统编码是什么而导入其他模块(例如 sys)。
I have a project in Python 2.6 and I'd like to write a utf-8 message to stdout using the system encoding. However it appears that such a function does not exist until Python 3.2:
PySys_FormatStdout
http://docs.python.org/dev/c-api/sys.html
Is there a way to do this from Python 2.6?
To clarify I have a banner that needs to print after Py_Initialize() and before the main interpreter is run. The string is a c-literal containing:
"\n and Copyright \xC2\xA9"
where \xC2\xA9 is the utf-8 copyright symbol. I verified in gdb that the copyright symbol is encoded correctly.
Update:
I just decided all this grief isn't necessary and I'm going to remove the offending character from the startup banner. There are just too many issues with this, and the documentation is lacking. My expectations were that this would be like Tcl, where:
- The embedded interpreter's C-API would make writing stdout out in unicode easy in the system's encoding, and not some default ascii encoding
- An exception wouldn't be thrown, if an offending character does not exist in the current encoding. Instead some default replacement character would be displayed.
- Additional modules, (e.g. sys), would not be necessary to import just to find out what the system encoding is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PyUnicode_DecodeUTF8()
PyObject_Print()
PyUnicode_DecodeUTF8()
PyObject_Print()
您可以使用 PyFile_WriteObject():
如果您知道最终的编码,那么你可以使用
PyUnicode_AsEncodedString()< /代码>
。
You could use PyFile_WriteObject():
If you know the final encoding then you could use
PyUnicode_AsEncodedString()
.