AttributeError:' int'对象没有属性'替换'打印时

时间:2018-05-20 20:20:08

标签: python

我收到错误

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

当我尝试打印b

的值时

如何打印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 个答案:

答案 0 :(得分:0)

您不能在Int对象上使用replace。

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

导致他出错,将其转换为str然后使用替换它。

答案 1 :(得分:0)

首先您需要将integer转换为string 之后,连接时也会出错

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

你还需要将your_list[i][1]转换为str,因为它会引发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)

试试这个:

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