所以我计划使用 Jupyter notbook (Python 3) 进行一些数据分析,出于协作原因,我想将数据存储在 github 存储库中,但是数据集很敏感。
因此,我想将数据(当前为 .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 中有所不同,因此代码会略有不同。
关于git - 将加密的 csv 导入 Python 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45358388/