Python错误:AttributeError:'NoneType'对象没有属性'to_excel' [英] Python Error: AttributeError: 'NoneType' object has no attribute 'to_excel'

查看:244
本文介绍了Python错误:AttributeError:'NoneType'对象没有属性'to_excel'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并目录中的所有文件,然后将合并的文件保存到另一个目录中.我正在使用Python 3.8.当我运行代码时,我得到以下带有AttributeError的错误:

I am trying to combine all files in a directory and the save the combined file into another directory. I am using Python 3.8. When I run the code I get the following with a AttributeError:

c:\test\Upload test\Book1.xlsx
c:\test\Upload test\Book2.xlsx
c:\test\Upload test\Book3.xlsx
Traceback (most recent call last):
File "C:/Python/PythonDev/combine.py", line 104, in <module>
newdf.to_excel(writer,"All")
AttributeError: 'NoneType' object has no attribute 'to_excel'

代码:

import pandas as pd
import globe
filelist = glob.glob(r'c:\test\Upload test\*.xlsx')
file1 = "*.*"
for i in  filelist:
    file2 = pd.read_excel(i)
    file2['FileName'] = i
    file1 = ['newdf']
    newdf = file1.append(file2)
    print (i)
writer = pd.ExcelWriter(r'c:\test\Uploaded\uploadfile.xlsx', engine= 'xlsxwriter')
newdf.to_excel(writer,"All")
writer.save()

推荐答案

追加未返回任何内容...

Append doesn't return anything...

尝试这样的事情:

import pandas as pd
import glob

raw_files = glob.glob(r'c:\test\Upload test\*.xlsx')

pd_files = pd.DataFrame()

for file in raw_files:
    pd_files.append(pd.read_excel(file))

pd_files.to_excel("c:\test\Uploaded\uploadfile.xlsx")

这篇关于Python错误:AttributeError:'NoneType'对象没有属性'to_excel'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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