Panda中的Excel输出 - TypeError:不可用类型:'list'

时间:2018-04-10 11:00:33

标签: python pandas beautifulsoup

我正在尝试在我正在创建的代码中解决此错误消息。当我运行它并打印(df)它给了我应该有的输出,但是当我尝试导出到.xlsx时它给了我这个错误:

  

Traceback(最近一次调用最后一次):文件   “C:/Attr_Editor_Excel/Extract_xlsx.py”,第38行,

     

df.to_excel(writer,sheet_name = sheet_name,startrow = 0,startcol = 0,   index = False,header = False)

     

文件“C:\ Python36 \ lib \ site-packages \ pandas \ core \ frame.py”,第1424行,   在to_excel中       发动机=发动机)

     

文件“C:\ Python36 \ lib \ site-packages \ pandas \ io \ formats \ excel.py”,行   628,在写       freeze_panes = freeze_panes)

     

文件“C:\ Python36 \ lib \ site-packages \ pandas \ io \ excel.py”,第1558行,   write_cells       如果是self.sheets中的sheet_name:

     

TypeError:不可用类型:'list'

我已经尝试了一个多小时来尝试解决此错误,但事实并非如此。所以,我正在尽力争取你们。

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  

1 个答案:

答案 0 :(得分:0)

"/home/dataset_1/control"

sheet_name = ([soup.find('leafname').text])是一个列表这一事实导致sheet_name失败,因为if sheet_name in self.sheets:是一个字典,这意味着self.sheets将尝试散列sheet_name in self.sheets因此错误sheet_name

TypeError: unhashable type: 'list'更改为字符串:

sheet_name
相关问题