Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python 3将LF打印到Windows标准输出_Windows_Python 3.x_Stdout - Fatal编程技术网

使用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.

对于嵌入Python并希望stdio使用默认文本模式的程序来说,这是一个错误
——谁会这样想,为什么?对我来说,这是Windows上的一个主要难题,因为它会破坏重定向的二进制流。将
wchar\u t
字符串打印到控制台的人希望使用文本模式,因为CRT至少会将它们转换为
char
(对于ASCII来说已经足够好了),而在二进制模式下,它只会写入原始的宽字符,我添加了一个ctypes示例,演示了在binary mode.OMG中调用CRT的
wprintf
函数的问题。一整罐虫子新行的自动转换可能是一个难以追踪的问题。当在atom内部查看输出时,我清楚地知道当我的
print
s似乎将换行符加倍时会发生什么,但我不知道如何禁用通用换行符转换——当然,我尝试直接调用
sys.stdout.write()
也失败了——离问题更近了一步,但仍然走错了方向。您重新定义sys.stdout的代码工作得非常好,谢谢。