I was reading about Bytes libraries and Pandas and I think the answers in the following post should work for me:
How to convert bytes data into a python pandas dataframe?
However, neither of them are working.
I have a CSV file with a few content. When I load it to Odoo, it returns the file into a Bytes
object. I want to read this Bytes
object with pandas
and convert it into a dataframe
in order to using dataframe
methods.
The Bytes
object comes in the attribute file_to_import
of my class (that is why you'll see self.file_to_import
in my code). If I show its type it returns <class 'bytes'>
. If I decode it, I get its respective string:
/UHJlY2lvIGRlIGNvbXByYSBkZSB0b2RvcyBsb3MgcHJvdmVlZG9yZXMgbyBzw7NsbyBkZSBlc3RlIHByb3ZlZWRvciBjb25jcmV0bz87O8K/Pzs7OztTdXN0aXR1aXIgQUlDIFNBIHBvciBzdSBjw7NkaWdvIHJhcm87Owo=
It looks OK, so this should be enough:
from io import BytesIO
import pandas as pd
df = pd.read_csv(BytesIO(self.file_to_import))
However, df
does not have any rows, and if I check df.empty
, it returns True
, so the dataframe
does not have any info. If I check the size of the BytesIO
object before trying to convert it into a dataframe
, it returns 1376 bytes, which seems to be OK, since Dolphin shows a size of 1,0 KiB (1.031) for the file.
x = BytesIO(self.file_to_import)
_logger.critical(x.getbuffer().nbytes)
df = pd.read_csv(x))
Can anyone tell me why is this happening? Why the dataframe
is empty?
Your string is base64
encoded. You need to decode it before to use:
import base64
s = b"ZGVmYXVsdF9jb2RlO2...Jhcm87Owo="
s = base64.decodebytes(s)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.