我正在关联一个帐号和我想要的输出文件的名称。我需要搜索一个文本文件之间的帐户123456789和'谢谢你的查询'抓取代码,并将其写入一个输出文件名为'你好.txt'. 你知道吗
我需要再次循环并提取'55555-55555'和'Thank you for your inquiry'之间的信息,然后将其写入名为'再见.txt'. 当我运行此代码时,没有任何内容写入我的文件。你知道吗
输入:
Account number: 123456789
Hi,
This is you bill. Please do not forget to include your account number on
your check.
If you have any further questions please feel free to contact me 1-800-325-
3232. Press
option 1 to reach my extension 1234.
Thank you for your inquiry
Account Number: 55555-55555
Hi,
This is you bill. Please do not forget to include your account number on
your check.
If you have any further questions please feel free to contact me 1-800-325-
3232. Press
option 1 to reach my extension 1234.
Thank you for your inquiry
我的剧本:
with open('SumBillRpt2019-2-27 Cyl 20.txt') as of:
for line in of.read().split('\n'):
for account, new_file in d.items():
with open(new_file, 'w') as nf:
if account in line:
writing = True
if writing:
nf.write(line)
print(nf)
if 'Thank you for your Inquiry' in line:
writing = False
输出应为:
你知道吗你好.txt你知道吗
12345-6789
some lines
Thank you for your inquiry
你知道吗再见.txt你知道吗
55555-55555
some lines
Thank you for your inquiry
也许像这样的方法会奏效:
另一种方法是使用re库:
要调用上述任一函数,请使用以下命令:
相关问题 更多 >
编程相关推荐