Issue
Someone I work with had a bstring and saved it to a file (with other stuff). Later, I open the file with pandas and try to read the bstring, but it has been converted to string:
import pandas as pd
# Saving value
bstring = b"\x0F\xC8\x3F\x7C\x00"
to_write = 'bstrings\n'
to_write += str(bstring)
with open('test.csv', "w") as f_csv:
f_csv.write(to_write)
# Reading value
my_df = pd.read_csv('test.csv')
bstring2 = my_df['bstrings'][0]
print(bstring)
print(type(bstring))
print(bstring2)
print(type(bstring2))
print(bstring == bstring2)
The output is :
b'\x0f\xc8?|\x00'
<class 'bytes'>
b'\x0f\xc8?|\x00'
<class 'str'>
False
bstring2 is now a string containing the characters b'\x0f\xc8?|\x00' (inclusing the b and the quotation marks), not a binary.
How do I transform back bstring2
to the binary bstring
?
I tried ctypes.fromhex
, which raises a ValueError
I found a few related questions but they did not seem to answer my question.
Solution
You can take any string representing a legal Python literal and convert it back to what it represents with ast.literal_eval
.
So in your code, just change:
bstring2 = my_df['bstrings'][0]
to:
bstring2 = ast.literal_eval(my_df['bstrings'][0])
adding an import ast
to the top of your file, and bstring2
will store the same value as bstring
.
Answered By - ShadowRanger
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.