Python出现AttributeError: 'NoneType'无count属性问题求助
Hey there, let's break down why this error is happening and how to fix it.
First, let's recap the error trace you ran into:
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'
What's Causing the Error?
This error pops up because one of the cells in your AsciiTable contains a None value. The terminaltables library tries to run count('\n') on every cell to calculate row height, but None doesn't have this method—hence the AttributeError.
Looking at your code, the None values are coming from the API response: some fields (like album title, language, release date, or artist names) might be missing or returned as null in the JSON. When you directly access these fields with x['title'] or y['name'], you get None instead of a string.
How to Fix It
We need to make sure every value added to your table data is a string (or at least not None). Here are the key changes to implement:
1. Add Default Values to Album List Fields
Modify the albums_list map to use dict.get() instead of direct key access. This lets you set a default value (like an empty string) if the field doesn't exist or is null:
albums_list = map(lambda x:[ x['album_id'], x.get('title', ''), # Default to empty string if title is missing x.get('language', ''), x.get('seokey', 'unknown-album'), # Avoid None for folder names x.get('release_date', ''), ','.join(map(lambda y: y.get('name', ''), x.get('artists',[])[:2])), # Handle missing artist names str(x.get('trackcount', 0)) # Convert track count to string, default to 0 ], albums)
2. Fix Track List Fields Too
Apply the same logic to the tracks_list to prevent None values in the track table:
tracks_list = map(lambda x:[ x.get('track_title', '').strip(), # Avoid error if track_title is None x['track_id'], x['album_id'], x.get('album_title', ''), ','.join(map(lambda y: y.get('name', ''), x.get('artist', []))), # Handle missing artist names x.get('duration', '') ], tracks)
3. Ensure Folder Name Safety
Since you use albums_list[idx][3] as the download folder name, setting a default like 'unknown-album' (as shown above) ensures you don't try to create a folder with a None name, which would cause another error later in the process.
Why This Works
By using dict.get(key, default_value), we guarantee that every value added to your tabledata is a string (or integer converted to string). The terminaltables library can then safely call count('\n') on all cells, eliminating the AttributeError entirely.
Give these changes a try, and your table should render without issues!
内容的提问来源于stack exchange,提问作者Steve

