<h2>大型文件解决方案:</h2>
<p>下面是一个工作示例,您可以通过在“必需字段”列表中添加或删除字段名来轻松自定义。
此解决方案允许您处理大量文件,因为整个文件不会同时读入内存。在</p>
<pre><code>import tempfile
# reproduce your input file
# for the purpose of having a
# working example
input_filename = None
with tempfile.NamedTemporaryFile(delete=False) as f_orig:
input_filename = f_orig.name
f_orig.write("""Name : ABC
Bank : Bank1
Account-No : 01234567
Amout: 123456
Spouse : CDF
Name : ABD
Bank : Bank1
Account-No : 01234568
Amout: 12345
Spouse : BDF
Name : ABE
Bank : Bank2
Account-No : 01234569
Amout: 12344
Spouse : CDG""")
# start looking from the beginning of the file again
f_orig.seek(0)
# list the fields you want to keep
required_fields = [
'Account-No',
'Amout',
]
# filter and write, line by line
result_filename = None
with tempfile.NamedTemporaryFile(delete=False) as f_result:
result_filename = f_result.name
# process one line at a time (memory efficient)
while True:
line = f_orig.readline()
#check if we have reached the end of the file
if not line:
break
for field_name in required_fields:
# write fields of interest to new file
if field_name in line:
f_result.write(line)
f_result.write('\n') # just for formatting
# show result
with open(result_filename, 'r') as f:
print(f.read())
</code></pre>
<p>其结果是:</p>
^{pr2}$