📅  最后修改于: 2023-12-03 15:15:03.129000             🧑  作者: Mango
If you have encrypted data using Fernet in Python and saved it in a CSV file, you may encounter difficulties with decrypting it using pandas. This guide will show you how to overcome this issue.
When attempting to decrypt strings in a CSV file using pandas, you may receive the following error message:
TypeError: Object type bytes is not supported in the pandas.to_csv function
This is because pandas does not know how to handle the encryption used by Fernet, which uses byte strings.
To decrypt the encrypted strings saved in the CSV file using Fernet, you can convert the byte strings to string objects using the decode()
method. Then, you can decrypt the strings using Fernet as usual.
Here is an example code snippet demonstrating the process:
from cryptography.fernet import Fernet
import pandas as pd
# Load the CSV file
df = pd.read_csv('encrypted_data.csv')
# Create a Fernet object with your key
key = 'your key here'.encode()
f = Fernet(key)
# Convert byte strings to strings and decrypt
df['encrypted_column'] = df['encrypted_column'].apply(lambda x: f.decrypt(x.encode()).decode())
In this example, encrypted_data.csv
is the CSV file containing the encrypted data, your key here
is the key used to encrypt the data, and encrypted_column
is the name of the column containing the encrypted data.
By using apply()
to convert and decrypt each value in the column, you can decrypt all the encrypted strings in the CSV file.
By converting the byte strings to string objects before decrypting with Fernet, you can successfully decrypt the encrypted data saved in a CSV file using pandas in Python.