python NoneType属性错误

时间:2018-05-18 16:42:36

标签: python

我得到“AttributeError:'NoneType'对象没有属性'count'”,代码如下。请帮助解决相同问题。

def search_albums(self, query, _dir = None):
    from pprint import pprint
    url = self.urls['search_albums_new']
    url = url.format(query = query)
    response = self._get_url_contents(url)
    albums = response.json()['album']
    if albums:
        albums_list = map(lambda x:[x['album_id'],x['title'], x['language'], x['seokey'], x['release_date'],','.join(map(lambda y:y['name'], x.get('artists',[])[:2])) ,x['trackcount']], albums)
        tabledata = [['S No.', 'Album Title', 'Album Language', 'Release Date', 'Artists', 'Track Count']]
        for idx, value in enumerate(albums_list):
            tabledata.append([str(idx), value[1], value[2], value[4], value[5], value[6]])
        table = AsciiTable(tabledata)
        print table.table
        idx = int(raw_input('Which album do you wish to download? Enter S No. :'))
        album_details_url = self.urls['album_details']
        album_details_url = album_details_url.format(album_id = albums_list[idx][0])
        response = requests.get(album_details_url , headers = {'deviceType':'AndroidApp', 'appVersion':'V5'})
        tracks = response.json()['tracks']
        tracks_list = map(lambda x:[x['track_title'].strip(),x['track_id'],x['album_id'],x['album_title'], ','.join(map(lambda y:y['name'], x['artist'])), x['duration']], tracks)
        print 'List of tracks for ', albums_list[idx][1]
        tabledata = [['S No.', 'Track Title', 'Track Artist']]
        for idy, value in enumerate(tracks_list):
            tabledata.append([str(idy), value[0], value[4]])
        tabledata.append([str(idy+1), 'Enter this to download them all.',''])
        table = AsciiTable(tabledata)
        print table.table
        print 'Downloading tracks to %s folder'%albums_list[idx][3]
        ids = raw_input('Please enter csv of S no. to download:')
        while not self._check_input(ids, len(tracks_list)) or not ids:
            print 'Oops!! You made some error in entering input'
            ids = raw_input('Please enter csv of S no. to download:')
        if not _dir:
            _dir = albums_list[idx][3]
        self._check_path(_dir)
        ids = map(int,map(lambda x:x.strip(),ids.split(',')))
        if len(ids) == 1 and ids[0] == idy + 1:
            for item in tracks_list:
                song_url = self._get_song_url(item[1], item[2])
                self._download_track(song_url, item[0].replace(' ','-').strip(), _dir)
        else:
            for i in ids:
                item = tracks_list[i]
                song_url = self._get_song_url(item[1], item[2])
                self._download_track(song_url, item[0].replace(' ','-').strip(), _dir)
    else:
        print 'Ooopsss!!! Sorry no such album found.'
        print 'Why not try another Album? :)'

错误:

  

回溯(最近一次呼叫最后一次):文件“a-dl.py”,第163行,在          d.search_albums(args.album)在search_albums中的文件“a-dl.py”,第116行       print table.table文件“C:\ Python27 \ lib \ site-packages \ terminaltables.py”,第337行,在表中       padded_table_data = self.padded_table_data文件“C:\ Python27 \ lib \ site-packages \ terminaltables.py”,第326行,in   padded_table_data       height = max(行中c的[c.count('\ n')]或[0])+ 1 AttributeError:'NoneType'对象没有属性'count'

1 个答案:

答案 0 :(得分:0)

很少受到打击和审判,我得到了答案。

我改变了以下

albums_list = map(lambda x:[x['album_id'],x['title'], x['language'], x['seokey'], x['release_date'],','.join(map(lambda y:y['name'], x.get('artists',[])[:2])) ,x['trackcount']], albums)
        tabledata = [['S No.', 'Album Title', 'Album Language', 'Release Date', 'Artists', 'Track Count']]
        for idx, value in enumerate(albums_list):
            tabledata.append([str(idx), value[1], value[2], value[4], value[5], value[6]])

        albums_list = map(lambda x:[x['album_id'],x['title'], x['language'], x['seokey'], x['release_date']], albums)
        tabledata = [['S No.', 'Album Title', 'Album Language', 'Release Date']]
        for idx, value in enumerate(albums_list):
            tabledata.append([str(idx), value[1], value[2], value[4]])

现在工作正常:)