yigecook
212 天前
3g 的话,用 python 操作流式处理,也不用每五分钟处理,设置 chunk 为 200M ,就好。
```python
import re
# 定义敏感数据的正则表达式模式
sensitive_data_pattern = re.compile(r'\b\d{16}\b')
def process_chunk(chunk):
# 使用正则表达式替换敏感数据
return sensitive_data_pattern.sub('****', chunk)
def process_log_file(input_file_path, output_file_path, chunk_size=200*1024*1024):
with open(input_file_path, 'rb') as input_file, open(output_file_path, 'wb') as output_file:
while True:
chunk = input_file.read(chunk_size)
if not chunk:
break
# 将字节数据转换为字符串进行处理
chunk_str = chunk.decode('utf-8', errors='ignore')
processed_chunk_str = process_chunk(chunk_str)
# 将处理后的字符串转换回字节数据
processed_chunk = processed_chunk_str.encode('utf-8')
output_file.write(processed_chunk)
# 使用示例
input_file_path = 'path/to/your/large_log_file.log'
output_file_path = 'path/to/your/processed_log_file.log'
process_log_file(input_file_path, output_file_path)
```
以上脚本的工作流程如下:
定义敏感数据的正则表达式模式,用于匹配和替换敏感数据。
process_chunk 函数会对读取的块进行处理,移除敏感数据。
process_log_file 函数会逐块读取输入日志文件,每次读取 200M 的数据,处理后写入到输出文件。
通过这种方式,处理过程不会占用超过 200M 的内存,同时也能够有效地移除日志中的敏感数据。请根据您的具体需求调整正则表达式模式和其他处理逻辑。