将加密的csv导入Python 3 [英] Import encrypted csv into Python 3
问题描述
所以我打算使用Jupyter notbook(Python 3)进行一些数据分析,出于协作原因,我想将数据存储在github repo上,但数据集是敏感的。
So I am planning to do some data analysis using a Jupyter notbook (Python 3), for collaborative reasons I want to store the data on a github repo, however the data set is sensitive.
因此,我想将数据(当前.csv)作为加密文件存储在repo上,然后在运行时将其解密(使用密码提示我猜)。
As such I would like to store the data (currently .csv) as an encrypted file on the repo and then decrypt it at runtime (with a password prompt I guess).
最好的方法是什么?
推荐答案
最后我用了python 3.6并 SimpleCrypt 加密该文件,然后将其上传。
In the end, I used python 3.6 and SimpleCrypt to encrypt the file and then uploaded it.
我认为这是我用来加密文件的代码:
I think this is the code I used to encrypt the file:
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:)
作为参数,所以我不必存储密码
变量内存
This is the code I use to decrypt at runtime, I run getpass("password: ")
as an argument so I don't have to store a password
variable in memory
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中是不同的,所以代码将略不同的。
Note, the UTF-8 encoding behaviour is different in python 2.7 so the code will be slightly different.
这篇关于将加密的csv导入Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!