将csv之类的文件读取到 pandas [英] Reading csv like file to pandas

查看:101
本文介绍了将csv之类的文件读取到 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Excel文件读入pandas,但收到消息format and extension of the file don't match.

I am trying to read an Excel file into pandas, but I get the message format and extension of the file don't match.

当我尝试使用read_excel时,收到一条错误消息,因此正在使用read_csv.

When I try to use read_excel, I get an error message, I am therefore using read_csv.

这是问题所在;我的"Excel喜欢"文件的某些行上有空单元格,并创建了一个奇怪的df,其中某些字段已移动:

This is where the issue is; my 'Excel like' file has empty cells on some rows, and it creates a weird df, where some field are shifted:


我的代码如下:

2010 = pd.read_csv(r'{0}\\file.xls'.format(path_temp),sep = 
r'\t*',encoding='iso-8859-2')

在输出中,列Outcome出现在第8行数据框的第6(日期4)列中.您知道解决方法吗?我需要每15分钟自动加载一次此文件,这意味着我想避免手动打开并另存为excel

In the output, column Outcome appears in 6th (date 4) column of the data frame from row 8. Would you know of a workaround? I need to load this file automatically every 15mins, meaning I d like to avoid a manual open and save as with excel

推荐答案

您的分隔符是一个正则表达式. sep=r'\t*'匹配任意数量的连续制表符,因此空白单元格应该被视为单个定界符.尝试使用sep='\t'.

Your separator is a regex. sep=r'\t*' matches any number of consecutive tabs, and so what should be blank cells get treated as a single delimiter. Try sep='\t' instead.

这篇关于将csv之类的文件读取到 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆