[英]Converting a Bytes object into a Pandas dataframe with Python3 results in an empty dataframe. Why?
I was reading about Bytes libraries and Pandas and I think the answers in the following post should work for me:我正在阅读有关 Bytes 库和 Pandas 的信息,我认为以下帖子中的答案应该对我有用:
How to convert bytes data into a python pandas dataframe? 如何将字节数据转换为 python pandas dataframe?
However, neither of them are working.但是,它们都没有工作。
I have a CSV file with a few content.我有一个包含一些内容的 CSV 文件。 When I load it to Odoo, it returns the file into a
Bytes
object.当我将它加载到 Odoo 时,它会将文件返回到
Bytes
object 中。 I want to read this Bytes
object with pandas
and convert it into a dataframe
in order to using dataframe
methods.我想用
pandas
阅读这个Bytes
object 并将其转换为dataframe
以便使用dataframe
方法。
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). Bytes
object 出现在我的 class 的属性file_to_import
中(这就是为什么你会在我的代码中看到self.file_to_import
的原因)。 If I show its type it returns <class 'bytes'>
.如果我显示它的类型,它会返回
<class 'bytes'>
。 If I decode it, I get its respective string:如果我解码它,我会得到它各自的字符串:
/UHJlY2lvIGRlIGNvbXByYSBkZSB0b2RvcyBsb3MgcHJvdmVlZG9yZXMgbyBzw7NsbyBkZSBlc3RlIHByb3ZlZWRvciBjb25jcmV0bz87O8K/Pzs7OztTdXN0aXR1aXIgQUlDIFNBIHBvciBzdSBjw7NkaWdvIHJhcm87Owo=
/UHJlY2lvIGRlIGNvbXByYSBkZSB0b2RvcyBsb3MgcHJvdmVlZG9yZXMgbyBzw7NsbyBkZSBlc3RlIHByb3ZlZWRvciBjb25jcmV0bz87O8K/Pzs7OztTdXN0aXR1aXIgQUlDIFNdHBvciBzdcm8w7WoK
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.但是,
df
没有任何行,如果我检查df.empty
,它会返回True
,因此dataframe
没有任何信息。 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.如果我在尝试将其转换为
dataframe
之前检查BytesIO
object 的大小,它会返回 1376 字节,这似乎没问题,因为 Dolphin 显示文件的大小为 1,0 KiB (1.031)。
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?为什么
dataframe
是空的?
Your string is base64
encoded.您的字符串是
base64
编码的。 You need to decode it before to use:您需要在使用之前对其进行解码:
import base64
s = b"ZGVmYXVsdF9jb2RlO2...Jhcm87Owo="
s = base64.decodebytes(s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.