因此,我计划使用 Jupyter Notebook (Python 3) 进行一些数据分析,出于协作原因,我想将数据存储在 github 存储库上,但数据集很敏感。
因此,我想将数据(当前为 .csv)作为加密文件存储在存储库上,然后在运行时解密(我猜有密码提示)。
做到这一点的最佳方法是什么?
最后,我使用了 python 3.6 和简单密码 https://pypi.python.org/pypi/simple-crypt加密文件然后上传。
I think这是我用来加密文件的代码:
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)
请注意,Python 2.7 中的 UTF-8 编码行为有所不同,因此代码会略有不同。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)