python文件查找跳过行 [英] python file seek skips lines

查看:124
本文介绍了python文件查找跳过行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内容如下的文件:

I have a file with content:

0x11111111
0x22222222
0x33333333
0x44444444

我正在使用以下命令逐行阅读

And I'm reading it line by line using:

f = open('test1', 'r')
print "Register CIP_REGS_CONTROL value:"
for i in range(4):
    content = f.read(11)
    f.seek(11, 1)
    print content

请注意,由于末尾有'\ n'字符,因此每行有11个字节.但是输出是:

Note that there're 11 bytes each line due to the '\n' char at the end. But the output is:

0x11111111

0x33333333

第一行和第三行之后有一个空行,我不知道为什么会这样.如果我删除每行中的"\ n",并将读取和查找的大小更改为10,我将得到:

There's an empty line after the 1st and 3rd line, I don't know why it's like that. If I delete the '\n' in each line, and change the size of reading and seeking to 10, I got:

0x11111111
0x33333333

2行也丢失了.有人可以帮忙吗?预先感谢.

2 lines are also missing. Anybody can help? Thanks in advance.

推荐答案

删除搜索请求.每个调用都跳过接下来的11个字节.即读取也会移动当前位置.

Remove your seek call. Each call is skipping the next 11 bytes. That is read also moves the current position.

这篇关于python文件查找跳过行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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