擅长:python、mysql、java
<p>这里的关键是如何逐字符迭代文件(而不是更简单的逐行迭代)。在</p>
<p>最简单的解决方案是将这两个文件完全读入内存并一起迭代。这可以通过<code>file.read()</code>调用和<code>zip()</code>内置函数来完成。这是因为大文件会导致内存不足。在</p>
<p>写出结果只是一个正常的<code>file.write()</code>调用。在</p>
<p>例如:</p>
<pre><code>with open('plaintext.text') as ptf:
plaintext = ptf.read()
with open('key.txt') as keyf:
key = keyf.read()
with open('output.txt') as f:
for plaintext_char, key_char in zip(plaintext, key):
# Do something to combine the characters
f.write(new_char)
</code></pre>