为什么我的 Python 脚本 return 一个对象作为字符串
Why does my Python script return an object as a string
我正在编写一个小 python 脚本来将两个 excel 文件合并为一个。这很简单,但我在 updateExcelFile()
方法中收到一个错误,指出 'str' 对象没有属性 'DACNum.' 此错误发生在 excel 文件的第 71 行,第一个空白行。有人能指出这是为什么吗?谢谢!
Here's the full traceback:
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
`
从函数中删除 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")
您最后的代码将 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 之前检查您是否正在处理 DACReport它的属性。
我正在编写一个小 python 脚本来将两个 excel 文件合并为一个。这很简单,但我在 updateExcelFile()
方法中收到一个错误,指出 'str' 对象没有属性 'DACNum.' 此错误发生在 excel 文件的第 71 行,第一个空白行。有人能指出这是为什么吗?谢谢!
Here's the full traceback:
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
`
从函数中删除 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")
您最后的代码将 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 之前检查您是否正在处理 DACReport它的属性。