遗世之光 发表于 2022-3-17 15:17:14

append方法添加字符串报错

      times = []
        a='123'
        times.append(a)
        print(a)

        for row in reader:
                date = str(row)
                times = times.append(date)
运行这个代码报错,前面添加a都没问题,但是添加date就报错,说date没有确定类型
   times = times.append(date)
AttributeError: 'NoneType' object has no attribute 'append'
请问这是什么问题呢?

isdkz 发表于 2022-3-17 15:24:27

append 是直接对原来的列表操作的,没有返回值,你不要把它的结果赋值回给变量


      times = []
      a='123'
      times.append(a)
      print(a)

      for row in reader:
                date = str(row)
                times.append(date)            # 注意这里

遗世之光 发表于 2022-3-17 15:33:08

isdkz 发表于 2022-3-17 15:24
append 是直接对原来的列表操作的,没有返回值,你不要把它的结果赋值回给变量




谢谢
页: [1]
查看完整版本: append方法添加字符串报错