Python : AttributeError : 'str'객체는 for 루프 내부에서 메서드를 호출 할 때 속성이 없습니다.

매튜 시주

for 루프에 의해 생성 된 이름을 사용하여 클래스 인스턴스에 액세스하려고하면

"AttributeError : 'str'object has no attribute".

저는 Python을 처음 사용하며 "모범 사례가 아님"을 용서해주십시오. 어떤 도움을 주시면 감사하겠습니다

class Csv:
    def __init__(self, invoicenum, customer, invoicedate, duedate, description, amount, productservice, invoiceclass):
        self.invoicenum = invoicenum
        self.customer = customer
        self.invoicedate = invoicedate
        self.duedate = duedate
        self.description = description
        self.amount = amount
        self.productservice = productservice
        self.invoiceclass = invoiceclass
        self.line = '{},{},{},{},{},{},{},{}\n'.format(invoicenum, customer, invoicedate,
                                                       duedate, description, amount, productservice, invoiceclass)

    def wite_to_db(self):
        mydb = mysql.connector.connect(host="localhost", user="root",
                                       passwd="passwd", database="invoice")
        mycursor = mydb.cursor()
        sql = "insert into invoice " "(Invoice_no, Customer, Invoice_Date, Due_date, Description, Amount, Product_service, Class)" "VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
        val = (self.invoicenum, self.customer, self.invoicedate, self.duedate,
               self.description, self.amount, self.productservice, self.invoiceclass)
        mycursor.execute(sql, val)
        mydb.commit()


x0 = Csv("1355", "Test1 User", "2020-05-03", "2020-12-31", 7095, 300, "Tithe", "General Fund")
x1 = Csv("1124", "Test2 User", "2020-05-03", "2020-12-31", 7095, 300, "Tithe", "General Fund")
x2 = Csv("14432", "Test3 User", "2020-05-03", "2020-12-31", 7095, 300, "Tithe", "General Fund")
x3 = Csv("1312", "Test4 User", "2020-05-03", "2020-12-31", 7095, 300, "Tithe", "General Fund")


line_range = range(4)

for line in line_range:
    line_instance = 'x' + str(line)
    print(line_instance)
    line_instance.write_to_db()
바룩 G.

line_instancestr 입니다. str 에는 write_to_db 라는 함수가 없습니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관