且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从.txt文件中读取第N行(Python)

更新时间:2023-08-26 18:35:22

类似的内容仅每30行显示一次:

Something like that will only display every 30 lines:

with open('C:/Email/file.txt') as f:
    cpt = 0
    for line in f:
        cpt += 1
        if cpt == 30:
            print(line)
            cpt = 0

您可以将line变量放在这样的列表中:

you can put line variable in a list like this:

all_lines = []
...
all_lines.append(line)

或者用一行代码甚至更好:

or even better with one line of code:

with open('C:/Email/file.txt') as f:
    all_lines = [v for i, v in enumerate(f, start=1) if i % 30 == 0]

然后,您可以将其应用于datesyz.

Then you can apply that to dates, y and z instead.