Mac pandas bug记录(路径及缓存问题-Excel file format cannot be determined,you must specify an engine manually.

最近在做网上爬取财务数据的小项目,调试代码的过程中遇到了一个pandas 运行bug:pandas读取文件需要指定engine的类型。

原因:路径及缓存问题

1.问题:pandas 读写文件报错,

Excel file format cannot be determined,you must specify an engine manually.

见下图:
Mac pandas bug记录(路径及缓存问题-Excel file format cannot be determined,you must specify an engine manually._第1张图片

2.主要原因有两个:

2.1.打开excel会有"~$“开头的缓存文件。
2.2.Mac 下的pands运行有”.DS_Store"缓存文件。

如果存在缓存文件或者其他pandas默认读取不了的文件,就会让我们指定pandas的engine了。

3.解决办法:正确确定路径和清除缓存文件

通过if 判断是否有缓存文件,然后通过os.remove()移除,这样后续的pandas读取就不会报bug了。

  for name in path_list:
        if name.endswith('.DS_Store'):
            os.remove(os.path.join(sub_comp , name))

    for name in path_list:
        if name.startswith('~$'):
            os.remove(os.path.join(sub_comp , name))

这样就可以把文件夹中缓存文件删除,后续读取用来做数据分析容易多了。

你可能感兴趣的:(pandas,bug,缓存,macos)