诗岑
2019-12-12 19:03
采纳率: 100%
浏览 1.8k
已采纳

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条回答 默认 最新

相关推荐 更多相似问题