使用Python 3将LF打印到Windows标准输出
如何将使用Python 3将LF打印到Windows标准输出,windows,python-3.x,stdout,Windows,Python 3.x,Stdout,如何将\n打印到Windows上的标准输出?此代码适用于Python 2,但不适用于Python 3: # set sys.stdout to binary mode on Windows import sys, os, msvcrt msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) # the length of testfile created with # python test_py3k_lf_print.py > test
\n
打印到Windows上的标准输出?此代码适用于Python 2,但不适用于Python 3:
# set sys.stdout to binary mode on Windows
import sys, os, msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
# the length of testfile created with
# python test_py3k_lf_print.py > testfile
# below should be exactly 4 symbols (23 0A 23 0A)
print("#\n#")
Python3已经在二进制模式下配置了标准I/O,但它有自己的I/O实现来进行换行翻译。您可以手动调用
sys.stdout.buffer.write
来使用二进制模式BufferedWriter
,而不是使用需要文本模式文件的print
。如果需要使用打印
,则需要一个不使用通用换行符的新文本I/O包装器。例如:
stdout = open(sys.__stdout__.fileno(),
mode=sys.__stdout__.mode,
buffering=1,
encoding=sys.__stdout__.encoding,
errors=sys.__stdout__.errors,
newline='\n',
closefd=False)
由于closefd
为false,因此关闭此文件不会关闭原始的sys.stdout
文件描述符。您可以通过打印(“#\n#”,file=stdout)
明确使用此文件,或替换sys.stdout=stdout
。原件可作为sys.\uu stdout\uu
提供
背景
Python3的io
模块旨在为抽象基类RawIOBase
、BufferedIOBase
和TextIOBase
中的所有类似文件的对象提供跨平台和跨实现(CPython、pypypy、IronPython、Jython)规范。它在\u pyio
模块中包含一个参考纯Python实现。原始io.FileIO
实现的共同点是一组低级POSIX系统调用,如read
和write
,这消除了CRT stdio不一致的问题。在Windows上,POSIX层只是CRT的低I/O层,但至少限于单个平台的特性
Windows的一个怪癖是在POSIX I/O层中使用非标准文本和二进制模式。Python通过始终使用二进制模式并在stdio文件描述符1上调用setmode
来解决这个问题
Python可以通过实现RawIOBase
的WinFileIO
注册子类来避免将Windows CRT用于I/O。在中有一个建议的补丁。另一个例子是模块,它实现了WindowsConsolerArroder
和WindowsConsoleRawWriter
类
1.这给嵌入Python并希望stdio使用默认文本模式的程序带来了问题。例如,在二进制模式下,打印宽字符串不再像在ANSI文本模式下那样强制转换为
char
,当然也不会像在UTF-16文本模式下那样使用WriteConsoleW
打印。例如:
stdout = open(sys.__stdout__.fileno(),
mode=sys.__stdout__.mode,
buffering=1,
encoding=sys.__stdout__.encoding,
errors=sys.__stdout__.errors,
newline='\n',
closefd=False)
Python 2.7.10(默认,2015年5月23日09:44:00)
win32上的[MSC v.1500 64位(AMD64)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>导入系统、操作系统、msvcrt、ctypes
>>>ctypes.cdll.msvcr90.wprintf(b'w\x00i\x00d\x00e\x00\n\x00')
宽的
5.
>>>msvcrt.setmode(sys.stdout.fileno(),os.O_二进制)
16384
>>>ctypes.cdll.msvcr90.wprintf(b'w\x00i\x00d\x00e\x00\n\x00')
我会
5.
Python3已经在二进制模式下配置了标准I/O,但它有自己的I/O实现来进行换行翻译。您可以手动调用
sys.stdout.buffer.write
来使用二进制模式BufferedWriter
,而不是使用需要文本模式文件的print
。如果需要使用打印
,则需要一个不使用通用换行符的新文本I/O包装器。例如:
stdout = open(sys.__stdout__.fileno(),
mode=sys.__stdout__.mode,
buffering=1,
encoding=sys.__stdout__.encoding,
errors=sys.__stdout__.errors,
newline='\n',
closefd=False)
由于closefd
为false,因此关闭此文件不会关闭原始的sys.stdout
文件描述符。您可以通过打印(“#\n#”,file=stdout)
明确使用此文件,或替换sys.stdout=stdout
。原件可作为sys.\uu stdout\uu
提供
背景
Python3的io
模块旨在为抽象基类RawIOBase
、BufferedIOBase
和TextIOBase
中的所有类似文件的对象提供跨平台和跨实现(CPython、pypypy、IronPython、Jython)规范。它在\u pyio
模块中包含一个参考纯Python实现。原始io.FileIO
实现的共同点是一组低级POSIX系统调用,如read
和write
,这消除了CRT stdio不一致的问题。在Windows上,POSIX层只是CRT的低I/O层,但至少限于单个平台的特性
Windows的一个怪癖是在POSIX I/O层中使用非标准文本和二进制模式。Python通过始终使用二进制模式并在stdio文件描述符1上调用setmode
来解决这个问题
Python可以通过实现RawIOBase
的WinFileIO
注册子类来避免将Windows CRT用于I/O。在中有一个建议的补丁。另一个例子是模块,它实现了WindowsConsolerArroder
和WindowsConsoleRawWriter
类
1.这给嵌入Python并希望stdio使用默认文本模式的程序带来了问题。例如,在二进制模式下,打印宽字符串不再像在ANSI文本模式下那样强制转换为
char
,当然也不会像在UTF-16文本模式下那样使用WriteConsoleW
打印。例如:
stdout = open(sys.__stdout__.fileno(),
mode=sys.__stdout__.mode,
buffering=1,
encoding=sys.__stdout__.encoding,
errors=sys.__stdout__.errors,
newline='\n',
closefd=False)
Python 2.7.10(默认,2015年5月23日09:44:00)
win32上的[MSC v.1500 64位(AMD64)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>导入系统、操作系统、msvcrt、ctypes
>>>ctypes.cdll.msvcr90.wprintf(b'w\x00i\x00d\x00e\x00\n\x00')
宽的
5.
>>>msvcrt.setmode(sys.stdout.fileno(),os.O_二进制)
16384
>>>ctypes.cdll.msvcr90.wprintf(b'w\x00i\x00d\x00e\x00\n\x00')
我会
5.
对于嵌入Python并希望stdio使用默认文本模式的程序来说,这是一个错误
——谁会这样想,为什么?对我来说,这是一件非常头痛的事