解密PGP文件时CSV Python中的多行列值

2024-05-29 11:26:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试解密一个.csv.pgp文件。我正在使用以下代码:

import pgpy
import csv
import re
emsg = pgpy.PGPMessage.from_file( path_to_encrypted_file)
key,_ = pgpy.PGPKey.from_file(path_to_asc_private_key)

with key.unlock(passphrase=passphrase):
    orig = key.decrypt(emsg).message

这段代码有效,我的文件被解密,但我得到的是字节数组的形式。我可以将字节数组转换为csv文件,但是,列可能有多行值,这一直是一个问题

下面是我用来将此字节数组转换为csv的代码:

data = orig.decode('utf-8').splitlines()
with open("BICoreStrengths.csv", "w",encoding='utf-16') as csv_file:
        writer = csv.writer(csv_file, delimiter='\t')

        for line in data:
            var = re.split(',', line)
            writer.writerow(var)

这有解决办法吗?如果pgpy模块有一个函数可以直接将文件解密为CSV,这也会很有帮助


Tags: 文件csvtopathkey代码fromimport

热门问题