EOF在二进制使用文件蟒 [英] EOF in a binary file using python
问题描述
我做了一个code读二进制文件如下:
I've made a code to read a binary file as follows :
file=open('myfile.chn','rb')
i=0
for x in file:
i=i+1
print(x)
file.close()
和结果如下(其中的一部分): B?\\ X00 \\ X00 \\ X80 \\ X00 \\ X00 \\ X00 \\ x005.xx \\ x00S \\ XD 4 \\ n'
and the result as follows (a part of it) : b'\x00\x00\x80?\x00\x00\x00\x005.xx\x00S\xd4\n'
我如何可以检测到这种二进制文件的EOF?让说,我想打印()后,我找到EOF。我想这一点,但什么都没有发生。
How can i detect the EOF of this binary file? Let say i want to print() after i find the EOF. I tried this, but nothing happened.
if (x=='\n'):
print()
(更新)
@aix:让说,该文件有效果的几行字,就像这个例子中,每一行都有的'\\ n'结尾,我希望把每行之间的空间。
@aix: let say that the file have few lines of results,just like the example, each line has '\n' at the end and i want put a space between each line.
b'\x00\x00\x80?\x00\x00\x00\x005.xx\x00S\xd4\n'
b'\x82\x93p\x05\xf6\x8c4S\x00\x00\xaf\x07j\n'
我怎样才能做到这一点?
How can i do this?
推荐答案
在您到达EOF,则在文件X:
循环会终止
Once your reach the EOF, the for x in file:
loop will terminate.
with open('myfile.chn', 'rb') as f:
i = 0
for x in f:
i += 1
print(x)
print('reached the EOF')
我已经改名为文件
变量,因此它不会影的内置。
I've renamed the file
variable so that it doesn't shadow the built-in.
这篇关于EOF在二进制使用文件蟒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!