诗岑 2019-12-12 19:03 采纳率: 93%
浏览 1919
已采纳

AttributeError: 'NoneType' object has no attribute 'append'

拟输出一个日期列表:

file=open("C:/Users/jyz_1/Desktop/新建文本文档.txt")
file=file.read()
lsy=[]
dates=file.split("\n")
for date in dates:
    while "-" in date:
        p1=date.index('-')#the first -
        p2=date.find('-',p1+1)#the second -
        month=date[p1+1:p2]
        day=date[p2+1:-1]
    date_on_x=int(month+day)
    lsy=lsy.append(date_on_x)

报错:

Traceback (most recent call last):
  File "C:\Users\jyz_1\AppData\Local\Programs\Python\Python37-32\32rx.py", line 26, in <module>
    lsy=lsy.append(t)
AttributeError: 'NoneType' object has no attribute 'append'

难道lsy不是列表?
在shell中输入:

>>> lsy=[]
>>> a='2015-1-1'
>>> lsy.append(a)
>>> lsy
['2015-1-1']

结果正常。
百思不得其解,求大神解惑。

  • 写回答

1条回答

  • python收藏家 2019-12-17 17:34
    关注

    lsy是列表,但是append方法是没有返回值的

    lsy=lsy.append(date_on_x) 改成 lsy.append(date_on_x) 就行了

    你在shell中输入的不对啊,输入lsy = lsy.append(a) 你再看

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程