如下txt文件,读取Shipper和Booking No.这两行。
Dear customer, please note that changes to payment terms or prepaid payer after an invoice has been made available may be subject to a payer amendment fee starting from the 1st of July
2019. In case you would like to create, view or modify your Standing instructions for payer, please click here - https://sealandmaersk.com/payer-standing-instructions/#/
Shipper
LONG SAIL INTERNATIONAL LOGISTICS CO., LTD.
12F, WENHUA BUILDING WEST, SHENNAN EAST ROAD, SHENZHEN,CHINA
TEL:0755-25190767
FAX:0755-25129435
Booking No.
205465283
Export references
Svc Contract
首先我们以只读方式(r)打开原文件。然后使用循环对每一行进行遍历,并使用enumerate函数(将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列)将读取到的每一行打包到enumerate对象中。然后进行判断我们所要读取的某几行,注意这个索引也是从0开始的。然后以追加的方式(a)写入文件中即可。
#读取指定内容
with open('./1.txt', 'r') as f:
for num, line in enumerate(f):
if num == 3:
print(line)
with open('./z.txt', 'a') as fp:
fp.write(line)
if num == 10:
print(line)
with open('./z.txt', 'a') as fp:
fp.write(line)
break