从文件创建键值对(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 + 1I 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 + 1As 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更多推荐
发布评论