如何在decrypt_file
中选择流作为gnupg
操作的输出?
文档和代码似乎表明这是不可能的。如果我是正确的(见下文),可能有哪些变通方法?
~~~
documentation 似乎暗示不可能:
decrypt_file(filename, always_trust=False, passphrase=None, output=None)¶
with “ output(str) - 用于将解密输出写入的文件名。”
~~~
打开代码,我看到了:
def decrypt_file(self, file, always_trust=False, passphrase=None,
output=None, extra_args=None):
args = ["--decrypt"]
if output: # write the output to a file with the specified name
self.set_output_without_confirmation(args, output)
if always_trust: # pragma: no cover
args.append("--always-trust")
if extra_args:
args.extend(extra_args)
result = self.result_map['crypt'](self)
self._handle_io(args, file, result, passphrase, binary=True)
logger.debug('decrypt result: %r', result.data)
return result
指向set_output_without_confirmation
,确认您传递的是字符串文件名:
def set_output_without_confirmation(self, args, output):
"If writing to a file which exists, avoid a confirmation message."
if os.path.exists(output):
# We need to avoid an overwrite confirmation message
args.extend(['--yes'])
args.extend(['--output', no_quote(output)])
答案 0 :(得分:0)
要将解密数据输出到变量使用decrypt
而不是decrypt_file
,如" 解密字符串&#中所示here 34;段。
原来的代码:
status = gpg.decrypt_file(input_file, passphrase='my_passphrase', output='my_output_file')
替换为:
decrypted_data = gpg.decrypt(input_file.read(), passphrase='my_passphrase')
# decrypted_data.data contains the data
decrypted_stream = io.BytesIO(decrypted_data.data)
# this is py3, in py2 BytesIO is imported from BytesIO
作为 csv数据的特定用例的示例,在此SO post的基础上,您可以这样做:
my_df = pandas.read_csv(decrypted_stream)