在只打开给定的data.txt文件一次的情况下,实现对文件内容的两次读取:第一次连续读取全部奇数行的数据并输出;第二次连续读取全部偶数行的数据,对数据进行utf-8编码后输出。
一下是文件的内容
this is the first line.
this is the second line.
this is the third line.
this is the fourth line.
this is the fifth line.
望采纳!谢谢
i = 1
j = 1
with open('data.txt', 'r') as f:
for one_data in f.readlines():
if i % 2==1:
print(one_data.encode('utf8'))
with open('data.txt', 'r') as f:
for one_data in f.readlines():
if j % 2==0:
print(one_data.encode('utf8'))