Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中通过加密读取和写入txt_Python_Python 3.x_Pandas - Fatal编程技术网

在python中通过加密读取和写入txt

在python中通过加密读取和写入txt,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有以下文件test.txt: 'campo1','campo2','campo3','campo4' '2222','34563434','547348568765756556','78967756K ' '2222','34564232','343575876567567584','65876566W ' '2222','54754456','234223144675987666','43453534A ' 我需要使用Crypto.Cipher库的函数

我有以下文件test.txt:

'campo1','campo2','campo3','campo4'    
'2222','34563434','547348568765756556','78967756K      '
'2222','34564232','343575876567567584','65876566W      '
'2222','54754456','234223144675987666','43453534A      '
我需要使用Crypto.Cipher库的函数DES3加密campo2、campo3和campo4。我编写了以下代码:

import pandas as pd
from Crypto.Cipher import DES3
infile = mainpath + "/" + "Prueba Encriptacion.txt"
outfile = mainpath + "/" + "Prueba Encriptacion out.txt"
cipher = DES3.new("4QGdtHLBSKmAJaOJY5BW")
df = pd.read_csv(infile, ',')
for row in df.iterrows():
    campo2_out= cipher.encrypt(campo2)
    campo3_out=cipher.encrypt(campo3)
    campo4_out=cipher.encrypt(campo4)

我的问题是,我不知道如何正确地遍历文件的行,并在输出文件中写入cipher.encrypt函数的结果。

在pandas中,您通常不会遍历这些行。将函数应用于所需的单元格,然后保存生成的数据帧

因此,您的代码应该是:

#... read frame as normal...
df["campo2"] = df["campo2"].apply(cipher.encrypt)
df["campo3"] = df["campo3"].apply(cipher.encrypt)
df["campo4"] = df["campo4"].apply(cipher.encrypt)
df.to_csv("outputfile)

我不得不修改一下你的代码,让它重现。但一旦成功,我就可以使用
applymap
,如下所示:

from Crypto.Cipher import DES3
from Crypto.Random import get_random_bytes

key = DES3.adjust_key_parity(get_random_bytes(24))
cipher = DES3.new(key, DES3.MODE_CFB)

df = pd.read_csv('test.txt', ',')
df = df.astype(str)
df = df.applymap(lambda x: cipher.encrypt(x.encode('UTF-8')))
给你:

campo1               campo2                                 campo3                                              campo4
0   b'\xe1#[\xd3'           b'\xe2\x9ee\xb4\xf1\xc4o\xa4'   b'w\xc0:\\Cn\xc7\x9f\xc4A\x93\x1e\x8c\xde\xa0\...   b'm>\xc2\xb3\xe3\xeb\xe6\\('
1   b'\x95\xea\xac\xed'     b'\xa6;\xfd\xb2\x98e\xd0\x8d'   b'01sVHg2j\xd0\x8f\xee\x90\x1a&\xd0\xae\xeb\xb3'    b'\xdb\x06\xcdh\x01\xce\xfdv\xca'
2   b'[\xfd\xf5A'           b'|\x85W\xe4\x19\x83(X'         b'\xb9E+\x00\xcf\\\xa2\r,\xa6\x9a\xf9\x0b[g\xe...   b'\xa4\xd2\x14&\x8c:\xf8N\xdc'

您可以使用python csv.reader读取csv,使用csv.writer写入

代码如下:

import csv
from Crypto.Cipher import DES3

cipher = DES3.new("4QGdtHLBSKmAJaOJY5BW")
infile = mainpath + "/" + "Prueba Encriptacion.txt"
outfile = mainpath + "/" + "Prueba Encriptacion out.txt"


def encrypt_csv(file_name):
    with open(file_name, 'r') as csv_file:
        with open(outfile, 'w') as out_file:
            reader = csv.reader(csv_file, delimiter=",")
            writer = csv.writer(out_file, delimiter=",")
            for row in reader:
                encrypted_data = [cipher.encrypt(data) for data in row]
                writer.writerow(encrypted_data)


if __name__ == '__main__':
    encrypt_csv(infile)

如何仅对campo2、campo3和campo4应用加密?您可以循环使用`加密的_数据=[cipher.encrypt(data)for data in row]`而不是使用列表理解并有选择地加密数据