代码之家  ›  专栏  ›  技术社区  ›  shunmuga prakash

attributeError:“int”对象在打印时没有属性“replace”[关闭]

  •  -6
  • shunmuga prakash  · 技术社区  · 6 年前

    我出错了

    AttributeError: 'int' object has no attribute 'replace'
    

    当我尝试打印 b

    如何打印 ?

    your_list=[[1010 ,2,3],[1010 ,7,8]]
    b = []
    c = []
    d = []
    for i in range(1,2):
        b = your_list[i][0]
        b = b.replace('1010 ','')
        print(b)
        c = b +","+your_list[i][1]+","+your_list[i][2]
        c = c.split(",")
        d.append(c)
    
    3 回复  |  直到 6 年前
        1
  •  0
  •   Druta Ruslan    6 年前

    首先你需要转换 integer string 之后,在连接时也会出现错误

    b +","+your_list[i][1]+","+your_list[i][2]

    你也需要转换 your_list[i][1] 因为它提高了 TypeError: must be str, not int 错误,因为我们可以连接str+int

    your_list=[[1010 ,2,3],[1010 ,7,8]]                                                                                                                                               
    
    
    b = []
    c = []
    d = []
    for i in range(1,2):
            b = str(your_list[i][0])
            b = b.replace('1010','')
            print(b)
            c = b +","+str(your_list[i][1])+","+str(your_list[i][2])
            c = c.split(",")
            d.append(c)
    
        2
  •  0
  •   Andrei Suvorkov    6 年前

    试试这个:

    a = "" // initiate a string
    b = your_list[i][0] // get a int value
    a = a + b // convert int to string
    a = a.replace('1010 ','') // replace 
    
        3
  •  0
  •   dee-see Tom Ritsema    6 年前

    您可以在int对象上使用replace。

    b = b.replace('1010 ','')
    

    它导致了错误,将其转换为str,然后对其使用replace。