代码之家  ›  专栏  ›  技术社区  ›  Jason Mc Clafferty

为什么我的Python脚本将对象作为字符串返回

  •  0
  • Jason Mc Clafferty  · 技术社区  · 7 年前

    我正在编写一个小python脚本,将两个excel文件合并为一个。这相当简单,但我在 updateExcelFile()

    以下是完整的回溯:

    Traceback (most recent call last):
    File "C:\Users\jasonmcclafferty\Desktop\Python\example.py", line 130, in 
    <module>
        DACMgr.updateExcelFile(x, row)
      File "C:\Users\jasonmcclafferty\Desktop\Python\example.py", line 109, 
    in updateExcelFile
        DACMgr.workbook.active.cell(row=c_row, column=1, 
    value=DACReport.DACNum)
    AttributeError: 'str' object has no attribute 'DACNum'
    [Finished in 3.3s with exit code 1]
    
    import openpyxl
    import os
    
    
    print("Current directory: " + os.getcwd())
    print()
    
    class DACReport:
        DACNum = ''
        PlanNum = ''
        CNNum = ''
        FSCNum = ''
        Name = ''
        AppDetails = ''
        Agent = ''
        DevAddress = ''
        DateRec = ''
        Deadline = ''
        Decision = '' 
        FIReq = ''
        Fee = ''
    
        def __init__(self, DACNum, Name):
            self.DACNum = DACNum
            self.Name = Name
    
        def __str__(self):
            return 'Application #: ' + self.DACNum + ' Applicant Name: ' + self.Name
    
    
    class DACReader:
    
        workbook = ''
    
        def __init__(self, workbook):
            self.workbook = workbook
    
        ## Reads the record at the given row, creates a DACEntry object from it and prints the string representation.
        def get_record_ar(self, c_row):
            w_bk = self.workbook
            w_sht = w_bk.active
    
    
            c_DAC = w_sht.cell(row=c_row, column=1).value
            c_Name = w_sht.cell(row=c_row, column=2).value
    
            issues = []
    
            if (c_DAC != None):
                if (c_Name == None):
                    issues.append(DACReport(c_DAC, c_Name))
    
                else:
                    tmp = DACReport(c_DAC, c_Name)
            else:
                return 0
    
            ## Object is printed and returned here, should also be written to excel
    
            if tmp != None:
                DACMgr.updateDACList(tmp)
    
                return tmp
    
        ## Reads the record at the given row, creates a DACRecord object from it and prints the string representation.
        def get_record_old(self, c_row):
            w_bk = self.workbook
            w_sht = w_bk.active
            tmp = ''
    
            c_DAC = w_sht.cell(row=c_row, column=5).value
            c_Name = w_sht.cell(row=c_row, column=6).value
    
            issues = []
    
            if (c_DAC != None):
                if (c_Name == None):
                    issues.append(DACReport(c_DAC, c_Name))
    
                else:
                    tmp = DACReport(c_DAC, c_Name)
            else:
                return 0
    
            ## Object is printed and returned here, should also be written to excel
            if tmp != None:
                DACMgr.updateDACList(tmp)
    
                return tmp
    
    
    class DACMgr:
    
        DAClist = []
    
        workbook = openpyxl.load_workbook("daccomplete.xlsx")
    
        def __init__(self, DAClist):
            self.DAClist = DAClist
    
        def updateDACList(DACReport):
            #print(DACReport)
            DACMgr.DAClist.append(DACReport)
            return 0
    
        # Updates the excel file with a DACReport at row = c_row
        def updateExcelFile(DACReport: DACReport, c_row: int):
                print(DACReport)
                DACMgr.workbook.active.cell(row=c_row, column=1, value=DACReport.DACNum)
                DACMgr.workbook.active.cell(row=c_row, column=2, value=DACReport.Name)
    
                openpyxl.writer.excel.save_workbook(DACMgr.workbook, "daccomplete.xlsx")    
    
    
    
    
    aReader = DACReader(openpyxl.load_workbook('ardalsdacregister.xlsx'))
    oReader = DACReader(openpyxl.load_workbook('olddacregister.xlsx'))
    
    
    for x in range(3, 360,1):
        oReader.get_record_old(x)
    
    for x in range(8,150,1):
        aReader.get_record_ar(x)
    
    row = 1
    for x in DACMgr.DAClist:
        print(x)
        DACMgr.updateExcelFile(x, row)
        row+=1
    

    `

    2 回复  |  直到 7 年前
        1
  •  0
  •   jcfollower    7 年前

    最后的代码发送 DAClist updateExcelFile

    for x in DACMgr.DAClist:
        print(x)
        DACMgr.updateExcelFile(x, row)
        row+=1
    

    同时 updateExcelFile value=DACReport.DACNum

    DAClist公司

    你的 get*

    DACMgr.updateDACList(tmp)
    

    tmp = '' .

    您可能应该将 tmp DACReport updateExcelFile 在尝试访问DACReport的一个属性之前,应该先检查是否正在处理它。

        2
  •  0
  •   Abhijeetk431    7 年前

    从函数中删除self并尝试。。。

    def updateExcelFile(DACReport: DACReport, c_row: int):
            print(DACReport)
            DACMgr.workbook.active.cell(row=c_row, column=1, value=DACReport.DACNum)
            DACMgr.workbook.active.cell(row=c_row, column=2, value=DACReport.Name)
    
            openpyxl.writer.excel.save_workbook(DACMgr.workbook, "daccomplete.xlsx")