添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
冲动的鸵鸟  ·  js 解析cron表达式 - ...·  2 年前    · 
火爆的茴香  ·  为 Azure-SSIS ...·  3 年前    · 

I have a io.BytesIO object, iostream , which is a be2 file read from disk, and I am going to append column headers to the table/ iostream ,

f = io.BytesIO()
f.write(b'A,B,C,D\n')
f.write(iostream.getvalue())
pd.read_table(f, sep=',', index_col=False, error_bad_lines=False, encoding='utf-8', dtype=type_map)

but it gave me an error,

pandas.errors.EmptyDataError: No columns to parse from file

I am wondering how to solve this issue.

Also tried

f = io.StringIO()
f.write('A,B,C,D\n')    
f.write(iostream.getvalue().decode())
pd.read_table(f, sep=',', index_col=False, error_bad_lines=False, encoding='utf-8', dtype=type_map)

got error

pandas.errors.ParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'.

I managed to reproduce your error. The problem that you have on your first try is that you are at the end of the stream 'f' at the moment of calling to 'pd.read_table', as you have just written all the contents. 'pd.read_table' calls internally to read(), which read from your current position. So it returns an empty string. This is the cause of error:

 pandas.errors.EmptyDataError: No columns to parse from file

The solution is simple. You just have to move again to the begining of the stream with 'seek'. This code worked for me:

f = io.BytesIO()
f.write(b'A,B,C,D\n')
f.write(iostream.getvalue())
f.seek(0)
pd.read_table(f, sep=',', index_col=False, error_bad_lines=False, encoding='utf-8')
        
            share
                    improve this answer