python对文件夹中CSV批量操作
时间: 2024-12-25 17:25:06 浏览: 14
Python可以用来执行文件夹中CSV文件的各种批量操作。比如,我们可以使用`merge_csv`脚本来合并所有CSV文件[^1]。对于MD5加密这样的操作,Python也有相应的工具库,如`pycryptodome`,可以实现对CSV文件的批量加密[^2]。以下是一个简单的示例:
1. **合并CSV文件**:
```python
import merge_csv
merge_csv.merge_folder_to_csv("/path/to/csv/folder", "/path/to/output.csv")
```
2. **批量MD5加密CSV文件**:
```python
from pycryptodome.cipher import AES
from Crypto.Util.Padding import pad, unpad
from os import path
def encrypt_csv(input_file, output_file):
with open(input_file, "rb") as f_in, open(output_file, "wb") as f_out:
data = f_in.read()
cipher = AES.new('secret_key', AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(pad(data, AES.block_size))
f_out.write(cipher.nonce + tag + ciphertext)
writefilename = os.path.join(os.path.dirname(__file__), "writecsv.csv")
encrypt_csv("/path/to/source/csvs/*.csv", writefilename)
```
其中,`secret_key`应替换为实际的密钥。
阅读全文