- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试做的事情的概述。我有需要读入 Pandas 的文件的加密版本。出于几个原因,解密成流而不是文件要好得多,所以这是我下面的兴趣,尽管我也尝试解密成文件只是作为中间步骤(但这也不起作用)。
我能够使它适用于 csv,但不适用于 hdf 或 stata(我接受适用于 hdf 或 stata 的答案,尽管两者的答案可能相同,这就是为什么我正在合并一个问题)。
加密/解密文件的代码取自另一个stackoverflow问题(目前我找不到)。
import pandas as pd
import io
from Crypto import Random
from Crypto.Cipher import AES
def pad(s):
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
def encrypt(message, key, key_size=256):
message = pad(message)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
return iv + cipher.encrypt(message)
def decrypt(ciphertext, key):
iv = ciphertext[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = cipher.decrypt(ciphertext[AES.block_size:])
return plaintext.rstrip(b"\0")
def encrypt_file(file_name, key):
with open(file_name, 'rb') as fo:
plaintext = fo.read()
enc = encrypt(plaintext, key)
with open(file_name + ".enc", 'wb') as fo:
fo.write(enc)
def decrypt_file(file_name, key):
with open(file_name, 'rb') as fo:
ciphertext = fo.read()
dec = decrypt(ciphertext, key)
with open(file_name[:-4], 'wb') as fo:
fo.write(dec)
这是我尝试扩展代码以解密到流而不是文件。
def decrypt_stream(file_name, key):
with open(file_name, 'rb') as fo:
ciphertext = fo.read()
dec = decrypt(ciphertext, key)
cipherbyte = io.BytesIO()
cipherbyte.write(dec)
cipherbyte.seek(0)
return cipherbyte
最后,这里是带有示例数据的示例程序,试图使这项工作成功:
key = 'this is an example key'[:16]
df = pd.DataFrame({ 'x':[1,2], 'y':[3,4] })
df.to_csv('test.csv',index=False)
df.to_hdf('test.h5','test',mode='w')
df.to_stata('test.dta')
encrypt_file('test.csv',key)
encrypt_file('test.h5',key)
encrypt_file('test.dta',key)
decrypt_file('test.csv.enc',key)
decrypt_file('test.h5.enc',key)
decrypt_file('test.dta.enc',key)
# csv works here but hdf and stata don't
# I'm less interested in this part but include it for completeness
df_from_file = pd.read_csv('test.csv')
df_from_file = pd.read_hdf('test.h5','test')
df_from_file = pd.read_stata('test.dta')
# csv works here but hdf and stata don't
# the hdf and stata lines below are what I really need to get working
df_from_stream = pd.read_csv( decrypt_stream('test.csv.enc',key) )
df_from_stream = pd.read_hdf( decrypt_stream('test.h5.enc',key), 'test' )
df_from_stream = pd.read_stata( decrypt_stream('test.dta.enc',key) )
不幸的是,我不认为我可以再压缩这段代码并仍然有一个完整的例子。
同样,我希望所有 4 条非工作行都能正常工作(hdf 和 stata 的文件和流),但我很高兴接受一个适用于单独的 hdf 流或单独的 stata 流的答案.
此外,我对其他加密替代方案持开放态度,我只是使用了一些我在 SO 上找到的现有的基于 pycrypto 的代码。我的工作明确需要 256 位 AES,但除此之外我是开放的,因此该解决方案不需要专门基于 pycrypto 库或上面的特定代码示例。
关于我的设置的信息:
python: 3.4.3
pandas: 0.17.0 (anaconda 2.3.0 distribution)
mac os: 10.11.3
最佳答案
最大的问题是 padding/unpadding 方法。它假定空字符不能成为实际内容的一部分。由于 stata/hdf
文件是二进制文件,因此使用我们使用的额外字节数(编码为字符)进行填充更安全。这个数字将在取消填充期间使用。
同样目前,read_hdf
不支持从对象等文件中读取,即使 API 文档声称支持。如果我们将自己限制为 stata
格式,则以下代码将执行您需要的操作:
import pandas as pd
import io
from Crypto import Random
from Crypto.Cipher import AES
def pad(s):
n = AES.block_size - len(s) % AES.block_size
return s + n * chr(n)
def unpad(s):
return s[:-ord(s[-1])]
def encrypt(message, key, key_size=256):
message = pad(message)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
return iv + cipher.encrypt(message)
def decrypt(ciphertext, key):
iv = ciphertext[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = cipher.decrypt(ciphertext[AES.block_size:])
return unpad(plaintext)
def encrypt_file(file_name, key):
with open(file_name, 'rb') as fo:
plaintext = fo.read()
enc = encrypt(plaintext, key)
with open(file_name + ".enc", 'wb') as fo:
fo.write(enc)
def decrypt_stream(file_name, key):
with open(file_name, 'rb') as fo:
ciphertext = fo.read()
dec = decrypt(ciphertext, key)
cipherbyte = io.BytesIO()
cipherbyte.write(dec)
cipherbyte.seek(0)
return cipherbyte
key = 'this is an example key'[:16]
df = pd.DataFrame({
'x': [1,2],
'y': [3,4]
})
df.to_stata('test.dta')
encrypt_file('test.dta', key)
print pd.read_stata(decrypt_stream('test.dta.enc', key))
输出:
index x y
0 0 1 3
1 1 2 4
在 python 3 中,您可以使用以下 pad
、unpad
版本:
def pad(s):
n = AES.block_size - len(s) % AES.block_size
return s + bytearray([n] * n)
def unpad(s):
return s[:-s[-1]]
关于python - 将文件解密为流并将流读入 pandas(hdf 或 stata),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34978703/
只是想知道我对组/数据集名称的自由程度如何,或者是否需要使名称简短(因此可读性较差)。这适用于包含许多具有许多重复名称的组和数据集的 HDF5 文件。一些 XML API 做 string inter
简短问题 当 Pandas 在 HDFStore 上工作时(例如: .mean() 或 .apply() ),它是否将内存中的完整数据作为 DataFrame 加载,还是将记录作为 Serie 进行处
我有几个不同的相关数据框(如果需要,可以使用 id 加入它们)。但是,我并不总是同时需要它们。 由于它们非常大,将它们存储在单独的 HDF 存储中是否有意义?或者当我处理同一文件中的其他帧时,携带“未
我似乎无法让它发挥作用。所有示例和线程都让人创建新的数据集。我只想更新已创建的数据集中的一个字段。 这是我所拥有的: h5_file = h5py.File(event_file_path, "r+"
我在 hadoop 上写入小文件时遇到一个奇怪的问题。下面是示例程序 public void writeFile(Configuration conf, String message, String
当我运行 hdf namenode -format 时,它想要删除目录 /home/hadoop/hadooptmpdata/dfs/name/current 但它没有权限执行此操作。如何授予它权限?
有没有办法使用 pandas HDF 存储获取 HDF 表的信息? 例如在 SQL 中有: SELECT COUNT(*) 我想阅读基本表格大小而不必自己加载表格。 最佳答案 试试这个: In [4]
在 pandas 下,每次我使用表格格式而不是固定格式时,我的内存消耗都会爆炸。 import numpy as np np.random.seed(seed=10) df = pd.DataFram
我正在处理大量数据集,每个数据集都是一个 pandas DataFrame,由于它们的大小,我需要从磁盘访问它们。从我读到的内容来看,HDF 似乎是与它们一起工作的好方法,但我对构建数据的最佳方法有点
我正在开发云服务器,我需要使用 blob 存储来存储 HDF 文件 ( http://www.hdfgroup.org/HDF5/ )。 与文件中的创建、读取、写入和修改数据元素相关的函数来自 HDF
我正在尝试将数据存储为 hdf 格式,并希望将默认数据类型设置为表,以便稍后查询。 我正在使用基本代码: import pandas as pd from numpy import random as
我最近在 Lubuntu 上安装了 Anacondas Python。 我正在尝试加载可在 Windows PC 上运行的 HDF 文件: In [14]: import pandas as pd I
我写了下面的脚本,但是我有一个内存消耗的问题,pandas 分配了超过 30 G 的内存,其中数据文件的总和大约是 18 G import pandas as pd import numpy as n
您好,我看到了一些以 HDF5 格式存储的科学数据,我想知道是否有任何 NoSQl 解决方案可以达到与 HDF5 相同的读/写性能。 我的示例的数据使用树结构(/root 然后/root/key 然后
我想知道如何获取 hdf 文件的列名(似乎存储在 hdf header 中);例如,一个文件可能有名为 [a,b,c,d] 的列,而另一个文件有 [a,b,c] 列,而另一个文件有 [b,e,r,z]
我想做什么? pd.read_csv(... nrows=###) 可以读取文件的前 n 行。我想在使用 pd.read_hdf(...) 时做同样的事情。 问题是什么? 我对 documentati
我想将数千张动物图像加载到 pandas df 中,添加特征并可能转换为 HDF。 我使用 cv2.imread() 尝试了以下方法 import cv2 import os import numpy
我想知道 HDF 套件中嵌入的 kafka 和 Confluence 套件中嵌入的 kafka 之间的差异,特别是模式注册工具。 最佳答案 https://registry-project.readt
我想知道 HDF 套件中嵌入的 kafka 和 Confluence 套件中嵌入的 kafka 之间的差异,特别是模式注册工具。 最佳答案 https://registry-project.readt
我使用 pandas 和 hdf5 文件来处理大量数据(例如 10GB 或更多)。我想使用表格格式,以便在读取数据时能够有效地查询数据。但是,当我想将数据写入 hdf 存储时(使用 DataFrame
我是一名优秀的程序员,十分优秀!