git - 将加密的 csv 导入 Python 3

标签 git python-3.x csv encryption jupyter-notebook

所以我计划使用 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/

相关文章:

Github 突然说 "fatal: Could not read from remote repository."

git - 克隆私有(private)存储库 (Github)

Git提交失败: "assertion ` g_type_from_name (name) == 0' failed"

python-3.x - Tensorboard 显示空白页(拒绝从 'http://localhost:6006/index.js' 执行脚本,因为它的 MIME 类型)

python - 是否可以在没有任何 python 库的情况下从 xlsx 文件中读取信息?

php - 在二维数组中分解多个以逗号分隔的字符串,然后获取所有唯一值

regex - 如何匹配除选项卡之外的所有内容(对于 git diff --word-diff-regex)

python - 根据名称分割线

python - 用逗号替换每行中的多个空格

python csv header