不能读ascii字符26? [英] cannot read ascii character 26?

查看:498
本文介绍了不能读ascii字符26?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本模式下在文件中写了一个流。

I wrote a stream in a file in text mode.

# python code
f = open("somewhere in my computer","w")
f.write("Hello\nWorld")
f.write(chr(26)) # writing ascii character #26 to file 
f.write("hhh")
f.close()

ASCII字符#26。我知道我应该用二进制模式打开文件。是ascii字符#26 EOF 字符。因为你知道没有这样的东西,e.i没有 EOF 字符。那么什么问题呢?这是一个操作系统相关的问题吗? (我试试这个在Microsoft Windows)。

I cannot read bytes after ASCII character #26. I know i should open file with binary mode. Is ascii character #26 EOF character. As you know there is not such thing , e.i there is no EOF character. So what is problem? Is this an operating system dependent issue? (I try this in Microsoft windows).

推荐答案

问题是因为windows。 0x1A是Ctrl-Z,DOS使用它作为文件结束标记。 Python使用Windows CRT函数_wfopen,它实现了Ctrl-Z is EOF语义。
如果你的文件不是128个字节的确切倍数,你需要一种方法来标记文本的结尾。此文章意味着Ctrl-Z的选择基于DEC使用的更旧的约定。

The problem is because of the windows. 0x1A is Ctrl-Z, and DOS used that as an end-of-file marker. Python uses the Windows CRT function _wfopen, which implements the "Ctrl-Z is EOF" semantics. If your file wasn't an exact multiple of 128 bytes, you needed a way to mark the end of the text. This article implies that the selection of Ctrl-Z was based on an even older convention used by DEC.

这篇关于不能读ascii字符26?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆