逐行读取文件

新建文件,命名为yesterday。输入以下内容并保存。

Yesterday, all my troubles seemed so far away
昨天,一切烦恼仿佛远在天边
Now it looks as though they're here to stay
可我如今却忧心忡忡
Oh, I believe in yesterday
哦,我宁愿相信昨天
Suddenly, I'm not half the man I used to be
刹那间,我不再是从前的我
There's a shadow hanging over me.
她的身影总挥之不去
Oh, yesterday came suddenly
哦,往昔在脑海浮现
Why she had to go I don't know she wouldn't say
为何她不辞而别,姗然离去
I said something wrong, now I long for yesterday
一定是我说错了什么,我只好静静等待昨天
Yesterday, love was such an easy game to play
昨天,爱情本是如此简单
Now I need a place to hide away
我如今却渴望逃避
Oh, I believe in yesterday
哦,我宁愿相信昨天
Why she had to go I don't know she wouldn't say
为何她不辞而别,姗然离去
I said something wrong, now I long for yesterday
一定是我说错了什么,我只好静静等待昨天
Yesterday, love was such an easy game to play
昨天,爱情本是如此简单
Now I need a place to hide away
而我如今却渴望逃避
Oh, I believe in yesterday
哦,我宁愿相信昨天
Mm mm mm mm mm
嗯嗯嗯 [10]

新建Python脚本,命名为file_op.py。开始编写脚本,逐行读取yesterday文件中的内容:

f = open('yesterday','r',encoding='utf-8')
count = 0
for line in f:
    if count == 9:
        print('-----分割线-------')
        count += 1
        continue
    print(line.strip())
    count += 1
f.close()

执行以上脚本将逐行读取yesterday文件中的内容,不读取第10行,以分割线代替。

作者:James

生命苦短,学海无涯。向伟大领袖学习,“自信人生二百年,会当水击三千里”。永远保持一颗热忱的心。

发表评论

邮箱地址不会被公开。 必填项已用*标注