关于项目的python3.7 AttributeError

时间:2018-12-21 17:16:46

标签: python

def showPlt(res):
    res1 = sorted(res.items(), key=lambda x: x[1], reverse=True)
    ranks = []
    freqs = []
    for rank, value in enumerate(res1):
        ranks.append(rank + 1)
        freqs.append(value[1])
        rank += 1
    plt.loglog(ranks, freqs)
    plt.xlabel('**', fontsize=14, fontweight='bold', fontproperties='SimHei')
    plt.ylabel('**', fontsize=14, fontweight='bold', fontproperties='SimHei')
    plt.grid(True)
    plt.show()

files_path = file_name(r'S:\****')
res = getCharacter(files_path)
showPlt(res)

错误消息如下:

Traceback (most recent call last):
File "M:/untitled/StoryOfStone.py", line 44, in <module>
    showPlt(res)
File "M:/untitled/StoryOfStone.py", line 28, in showPlt
    res1 = sorted(res.items(), key=lambda x: x[1], reverse=True)
AttributeError: 'NoneType' object has no attribute 'items'

我的函数getCharacter()如下:

def getCharacter(files_path):
res = {}
for i in  range(1,len(files_path)):
    path = files_path[i]
    for x in open(path,'rb').read().decode('utf-8'):
        if 19968 <= ord(x) <= 40869:
            res[x] = res.get(x,0) + 1

我真的不知道items()出了什么问题。我的python版本是3.70。如果有人能解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:0)

这意味着resNone,因此您不能在其上调用任何常用方法。确保您的getCharacter方法从不返回None,然后重试。