从文件创建键值对(Creating key value pairs from file)

编程入门 行业动态 更新时间:2024-10-23 17:30:19
从文件创建键值对(Creating key value pairs from file) python

我有以下脚本读取整个file.bin文件,如何修改以读取文件的前1000行? 谢谢。

with open("file.bin","r") as text: for line in text: if line.startswith('MH =') or line.startswith('MN ='): value, key = line.strip().split('=') res[key] = value if num < 1000: print '%s : %s' % (key, value) num = num + 1

I have the following script that reads the entire file.bin file, how do I amend to read the first 1000 lines of the file instead? Thanks.

with open("file.bin","r") as text: for line in text: if line.startswith('MH =') or line.startswith('MN ='): value, key = line.strip().split('=') res[key] = value if num < 1000: print '%s : %s' % (key, value) num = num + 1

最满意答案

当你的数量达到1000时,打破;

ctr = 0; for line in text: ctr = ctr + 1 if ctr > 1000: break if line.startswith('MH =') or line.startswith('MN ='): value, key = line.strip().split('=') res[key] = value if num < 1000: print '%s : %s' % (key, value) num = num + 1

As your num reaches 1000, break;

ctr = 0; for line in text: ctr = ctr + 1 if ctr > 1000: break if line.startswith('MH =') or line.startswith('MN ='): value, key = line.strip().split('=') res[key] = value if num < 1000: print '%s : %s' % (key, value) num = num + 1

更多推荐

本文发布于:2023-08-03 12:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390198.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:键值   文件   Creating   file   pairs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!