Python Panda-TypeError中的Excel输出:不可损坏的类型:';列表'; 我试图在创建过程中的代码中排除这个错误信息。当我运行它并打印(df)时,它会给我应该有的输出,但是当我尝试导出到.xlsx时,它会给我以下错误:

Python Panda-TypeError中的Excel输出:不可损坏的类型:';列表'; 我试图在创建过程中的代码中排除这个错误信息。当我运行它并打印(df)时,它会给我应该有的输出,但是当我尝试导出到.xlsx时,它会给我以下错误:,python,pandas,beautifulsoup,Python,Pandas,Beautifulsoup,回溯(最近一次调用上次):文件 “C:/Attr\u Editor\u Excel/Extract\u xlsx.py”,第38行,在 df.to_excel(编写器,工作表名称=工作表名称,startrow=0,startcol=0, 索引=假,标题=假) 文件“C:\Python36\lib\site packages\pandas\core\frame.py”,第1424行, 以超越 发动机=发动机) 文件“C:\Python36\lib\site packages\pandas\io\f

回溯(最近一次调用上次):文件 “C:/Attr\u Editor\u Excel/Extract\u xlsx.py”,第38行,在

df.to_excel(编写器,工作表名称=工作表名称,startrow=0,startcol=0, 索引=假,标题=假)

文件“C:\Python36\lib\site packages\pandas\core\frame.py”,第1424行, 以超越 发动机=发动机)

文件“C:\Python36\lib\site packages\pandas\io\formats\excel.py”,第行 628,书面形式 冻结窗格=冻结窗格)

文件“C:\Python36\lib\site packages\pandas\io\excel.py”,第1558行,在 写入单元 如果self.sheets中的工作表名称:

TypeError:不可损坏的类型:“列表”

我已经尝试了一个多小时来解决这个错误,但是没有。所以我在做我最后的办法来问你们

from bs4 import BeautifulSoup
import os
import glob
import pandas as pd

os.chdir('C:/Users/PCTR261010/Desktop/Attribute Convert')
FileList = glob.glob('*.txt')

for fname in FileList:
    soup = BeautifulSoup(open(fname), 'html.parser')

# START----CODE-----CODE-----CODE----CODE-----CODE-----CODE----CODE-----CODE-----CODE
    attributes = dict()

    attributes['Description']=([soup.find('description').text])
    attributes['Date']=([soup.find('date').text])
    attributes['TIME']=([soup.find('time').text])
    attributes['MPCC']=([soup.find('leafname').text])

    #still working on this, needs to be column headers in output
    attributes['A']=([x.find('name').text for x in soup.find_all('attributedefinition')])
    attributes['B']=([x.find('displayname').text for x in soup.find_all('attributedefinition')])

    df = pd.DataFrame(list(attributes.items()))

    data = soup.find('data').text #product data

    for partno in data.split('\n'):
        if partno:
            attributes['partno']=(partno.split('\t'))

# END----CODE-----CODE-----CODE----CODE-----CODE-----CODE----CODE-----CODE-----CODE

    report_path = 'Attribute Excel Files'
    if not os.path.exists(report_path):
        os.makedirs(report_path)

        sheet_name = ([soup.find('leafname').text])
        writer = pd.ExcelWriter(os.path.join(fname + '.xlsx'), engine='xlsxwriter')
        df.to_excel(writer, sheet_name= sheet_name, startrow=0, startcol=0, index=False, header=False)

        workbook = writer.book
        workbook.filename = (fname + '.xlsx')
        worksheet = writer.sheets[sheet_name]  # Names sheet name

        writer.save()
样本数据

<HEADER>
<OPERATOR> NAPA PCC Export </OPERATOR>
<DESCRIPTION>  3686281-PFQ,Motor Oil - 1 qt  </DESCRIPTION>
<EMAIL>  </EMAIL>
<DATE> Wednesday, April 04, 2018 </DATE>
<TIME> 08:09 AM </TIME>
</HEADER>

<leafname>3686281</leafname>
<attributes>
<attributedefinition>
<name>Partnumber</name>
<displayname>Part number</displayname>
<guidetext>Part number and line abbreviation</guidetext>
</attributedefinition>

<attributedefinition>
<name>GPC_highmileageformula</name>
<displayname>High Mileage Formula</displayname>
<guidetext>High Mileage Formula</guidetext>
<values>
<domaintype>hard</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>GPC_synthetic</name>
<displayname>Synthetic</displayname>
<guidetext>Synthetic</guidetext>
<values>
<domaintype>hard</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>GPC_motoroilbrand</name>
<displayname>Brand</displayname>
<guidetext>Motor Oil Brand</guidetext>
<values>
<domaintype>hard</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>GPC_motoroilsubbrand</name>
<displayname>Sub Brand</displayname>
<guidetext>Motor Oil Sub Brand</guidetext>
<values>
<domaintype>hard</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_28892</name>
<displayname>Manufacturer Part Number</displayname>
<guidetext>Mfg Part Number</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>GPC_containersize</name>
<displayname>Container Size</displayname>
<guidetext>Container Size</guidetext>
<values>
<domaintype>hard</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>GPC_motoroilweightrating</name>
<displayname>Motor Oil Weight Rating</displayname>
<guidetext>Motor Oil Weight Rating</guidetext>
<values>
<domaintype>hard</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>GPC_motoroilstyleandtype</name>
<displayname>Motor Oil Style and Type</displayname>
<guidetext>Motor Oil Style and Type</guidetext>
<values>
<domaintype>hard</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>GPC_motoroilapirating</name>
<displayname>Motor Oil API Rating</displayname>
<guidetext>Motor Oil API Rating</guidetext>
<values>
<domaintype>hard</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_3534119</name>
<displayname>Motor Oil Specifications</displayname>
<guidetext>Motor Oil Specifications</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>GPC_dexosrating</name>
<displayname>Dexos Rating</displayname>
<guidetext>Dexos Rating</guidetext>
<values>
<domaintype>hard</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_23355</name>
<displayname>Product Features</displayname>
<guidetext>Product Features</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_2388</name>
<displayname>Packaging</displayname>
<guidetext>Type of Packaging</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_26544</name>
<displayname>Features & Benefits</displayname>
<guidetext>Features & Benefits</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_26689</name>
<displayname>Features & Benefits</displayname>
<guidetext>Features & Benefits</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_26974</name>
<displayname>Features & Benefits</displayname>
<guidetext>Features & Benefits</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_27547</name>
<displayname>Features & Benefits</displayname>
<guidetext>Features & Benefits - 2</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_27548</name>
<displayname>Features & Benefits</displayname>
<guidetext>Features & Benefits - 3</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_27549</name>
<displayname>Features & Benefits</displayname>
<guidetext>Features & Benefits - 4</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_27550</name>
<displayname>Features & Benefits</displayname>
<guidetext>Features & Benefits - 5</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_27551</name>
<displayname>Features & Benefits</displayname>
<guidetext>Features & Benefits - 6</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_27552</name>
<displayname>Features & Benefits</displayname>
<guidetext>Features & Benefits - 7</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_27553</name>
<displayname>Features & Benefits</displayname>
<guidetext>Features & Benefits - 8</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

<attributedefinition>
<name>PHdb_27554</name>
<displayname>Features & Benefits</displayname>
<guidetext>Features & Benefits - 9</guidetext>
<values>
<domaintype>soft</domaintype>
<VALUELIST>
</VALUELIST>
</values>
</attributedefinition>

</attributes>


<DATA>
PF0W20DEXQT     Yes NAPA    Proformer       1 qt    0W20    Synthetic   SN  ILSAC GF-5  Dexos 1 Gen 2   NAPA PROFORMER 0W20 SYNTHETIC DEXOS® MOTOR OIL is a superiorquality synthetic motor oil specially formulated from 100% pure virgin base oil and highperformance additives.  Bottle                                              
PF0W20SYQT      Yes NAPA    Proformer       1 qt    0W20    Synthetic   SN  ILSAC GF-5      NAPA PROFORMER 0W20 SYNTHETIC MOTOR OIL is a superior?quality synthetic motor oil specially formulated from 100% pure virgin base oil and highperformance additives.    Bottle  

NAPA PCC出口
3686281-PFQ,机油-1夸脱
2018年4月4日,星期三
上午8:09
3686281
零件号
零件号
零件号和行缩写
GPC_高里程公式
高里程公式
高里程公式
坚硬的
GPC_合成
合成的
合成的
坚硬的
GPC_汽车品牌
烙印
机油品牌
坚硬的
GPC_Motoroil子品牌
子品牌
机油子品牌
坚硬的
PHdb_28892
制造商零件号
制造商零件号
软的
GPC_集装箱化
容器大小
容器大小
坚硬的
GPC_电机额定功率
机油重量额定值
机油重量额定值
坚硬的
GPC_电机油样式和类型
机油类型和类型
机油类型和类型
坚硬的
GPC(通用汽车油库)
机油API等级
机油API等级
坚硬的
PHdb_3534119
机油规格
机油规格
软的
GPC_dexosrating
德克索斯评级
德克索斯评级
坚硬的
PHdb_23355
产品特点
产品特点
软的
PHdb_2388
包装
包装类型
软的
PHdb_26544
特点和优点
特点和优点
软的
PHdb_26689
特点和优点
特点和优点
软的
PHdb_26974
特点和优点
特点和优点
软的
PHdb_27547
特点和优点
功能和优点-2
软的
PHdb_27548
特点和优点
功能和优点-3
软的
PHdb_27549
特点和优点
特点和优点-4
软的
PHdb_27550
特点和优点
特点和优点-5
软的
PHdb_27551
特点和优点
特点和优点-6
软的
PHdb_27552
特点和优点
特点和优点-7
软的
PHdb_27553
特点和优点
功能和优点-8
软的
PHdb_27554
特点和优点
特点和优点-9
软的
PF0W20DEXQT Yes NAPA Proformer 1 qt 0W20合成SN ILSAC GF-5 DEXS 1 Gen 2 NAPA Proformer 0W20合成DEXS®机油是一种优质合成机油,由100%纯基础油和高性能添加剂制成。瓶子
PF0W20SYQT Yes NAPA Proformer 1 qt 0W20合成SN ILSAC GF-5 NAPA Proformer 0W20合成机油是一种优质合成机油,专门由100%纯基础油和高性能添加剂配制而成。瓶子
sheet_name=([soup.find('leafname').text])

sheet\u name
是一个列表的事实导致
如果self.sheets:
中的sheet\u name失败,因为
self.sheets
是一个字典,意思是self.sheets中的
sheet\u name将尝试哈希
sheet\u name
因此出现错误
TypeError:unhable type:'list'

将图纸名称更改为字符串:

sheet_name = soup.find('leafname').text

当然,那样就行了。谢谢你发现了我的错误,我不能