Har*_*rvs 2 csv git encryption python-3.x jupyter-notebook
因此,我计划使用 Jupyter notbook (Python 3) 进行一些数据分析,出于协作原因,我想将数据存储在 github 存储库中,但是数据集很敏感。
因此,我想将数据(当前为 .csv)作为加密文件存储在 repo 上,然后在运行时对其进行解密(我猜是密码提示)。
执行此操作的最佳方法是什么?
最后,我使用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
Run Code Online (Sandbox Code Playgroud)
这是我在运行时用来解密的代码,我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)
Run Code Online (Sandbox Code Playgroud)
请注意,UTF-8 编码行为在 python 2.7 中有所不同,因此代码会略有不同。