如何使用python一次从文件中读取两行 [英] How do I read two lines from a file at a time using python

查看:194
本文介绍了如何使用python一次从文件中读取两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个解析文本文件的 python 脚本.这个文本文件的格式是这样的,文件中的每个元素都使用两行,为了方便起见,我想在解析之前阅读这两行.这可以在 Python 中完成吗?

I am coding a python script that parses a text file. The format of this text file is such that each element in the file uses two lines and for convenience I would like to read both lines before parsing. Can this be done in Python?

我想要一些类似的东西:

I would like to some something like:

f = open(filename, "r")
for line in f:
    line1 = line
    line2 = f.readline()

f.close

但是这打破了:

ValueError:混合迭代和读取方法会丢失数据

ValueError: Mixing iteration and read methods would lose data

相关:

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