簡體   English   中英

Python 錯誤:AttributeError:'NoneType' object 沒有屬性'to_excel'

[英]Python Error: AttributeError: 'NoneType' object has no attribute 'to_excel'

我正在嘗試合並一個目錄中的所有文件並將合並的文件保存到另一個目錄中。 我正在使用 Python 3.8。 當我運行代碼時,我得到以下帶有 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 沒有返回任何東西......

嘗試這樣的事情:

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM