如何将字节数据转换为python pandas数据框? [英] How to convert bytes data into a python pandas dataframe?
本文介绍了如何将字节数据转换为python pandas数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将字节"数据转换为Pandas数据框.
I would like to convert 'bytes' data into a Pandas dataframe.
数据如下所示(前几行):
The data looks like this (few first lines):
(b'#Settlement Date,Settlement Period,CCGT,OIL,COAL,NUCLEAR,WIND,PS,NPSHYD,OCGT'
b',OTHER,INTFR,INTIRL,INTNED,INTEW,BIOMASS\n2017-01-01,1,7727,0,3815,7404,3'
b'923,0,944,0,2123,948,296,856,238,\n2017-01-01,2,8338,0,3815,7403,3658,16,'
b'909,0,2124,998,298,874,288,\n2017-01-01,3,7927,0,3801,7408,3925,0,864,0,2'
b'122,998,298,816,286,\n2017-01-01,4,6996,0,3803,7407,4393,0,863,0,2122,998'
列标题显示在顶部.接下来的每一行都是一个时间戳和数字.
The columns headers appear at the top. each subsequent line is a timestamp and numbers.
有直接的方法吗?
非常感谢
@Paula Livingstone:
@Paula Livingstone:
这似乎可行:
s=str(bytes_data,'utf-8')
file = open("data.txt","w")
file.write(s)
df=pd.read_csv('data.txt')
也许可以在不使用文件的情况下完成此操作.
maybe this can be done without using a file in between.
推荐答案
我遇到了同样的问题,发现该库 https://docs.python.org/2/library/stringio.html 从这里的答案中:
I had the same issue and found this library https://docs.python.org/2/library/stringio.html from the answer here: How to create a Pandas DataFrame from a string
尝试类似的东西:
from io import StringIO
s=str(bytes_data,'utf-8')
data = StringIO(s)
df=pd.read_csv(data)
这篇关于如何将字节数据转换为python pandas数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文