[英]Why does my python function not return the correct times the letters are in the string?
[英]Why does my Python script return an object as a string
我正在写一个小的python脚本,将两个excel文件合并为一个。 这很简单,但是在updateExcelFile()
方法内部出现错误,指出“ str”对象没有属性“ DACNum”。 在Excel文件的第71行(第一空白行)上会发生此错误。 有人可以指出为什么吗? 谢谢!
这是完整的追溯:
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
`
从功能中删除自我并尝试...
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")
您的代码最后将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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.