python类型属性

2024-03-29 12:21:31 发布

您现在位置: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? :)'

错误:

Traceback (most recent call last): File "a-dl.py", line 163, in d.search_albums(args.album) File "a-dl.py", line 116, in search_albums print table.table File "C:\Python27\lib\site-packages\terminaltables.py", line 337, in table padded_table_data = self.padded_table_data File "C:\Python27\lib\site-packages\terminaltables.py", line 326, in padded_table_data height = max([c.count('\n') for c in row] or [0]) + 1 AttributeError: 'NoneType' object has no attribute 'count'


Tags: inselfidsurlalbumvaluedirtable
1条回答
网友
1楼 · 发布于 2024-03-29 12:21:31

好吧,我得到了答案。你知道吗

我改变了主意

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]])

现在效果很好:)

相关问题 更多 >