导入加密的csv到Python 3
问题描述:
因此,我打算使用Jupyter Notbook(Python 3)进行一些数据分析,出于协作原因,我想将数据存储在github repo上,但数据集很敏感。导入加密的csv到Python 3
因此,我想将数据(当前为.csv)作为加密文件存储在回购站中,然后在运行时解密(我猜想有密码提示)。
这样做的最佳方法是什么?
答
最后,我用python 3.6和SimpleCrypt来加密文件,然后上传它。
我认为这是我用来加密该文件的代码:
f = open('file.csv','r').read()
ciphertext = encrypt('USERPASSWORD',f.encode('utf8')) #this .encode('utf8') is the bit im unsure about
e = open('file.enc','wb') # file.enc doesn't need to exist, python will create it
e.write(ciphertext)
e.close
这是我在运行时使用解密代码,我跑getpass("password: ")
作为参数,所以我不必一个password
变量存储在存储器
from io import StringIO
import pandas as pd
from simplecrypt import encrypt, decrypt
from getpass import getpass
# opens the file
f = open('file.enc','rb').read()
print('Please enter the password and press the enter key \n Decryption may take some time')
# Decrypts the data, requires a user-input password
CSVplaintext = decrypt(getpass("password: "), f).decode('utf8')
print('Data have been Decrypted')
#create a temp csv-like file to pass to pandas.read_csv()
DATA=StringIO(CSVplaintext)
# Makes a panda dataframe with the data
df = pd.read_csv(DATA)
注意,UTF-8编码的行为是在python 2.7不同,使代码会稍有不同。
可能出现[如何使用Python/PyCrypto以OpenSSL兼容方式对文件进行AES加密/解密?](https://*.com/questions/16761458/how-to-aes-encrypt-decrypt-files - 使用 - 蟒-pycrypto功能于一个-OpenSSL兼容) –