使用 ast.literal_eval 或 JSON 解析字典时出错.编写字符串时可能存在错误?蟒



我试图解析从 Python3 中的 Spotify 专辑 API 查询的字典,但收到错误消息:ValueError: malformed node or string使用ast.literal_eval时。使用JSON.loads时,我收到错误消息:SyntaxError: EOL while scanning string literal.

在查看字典时,我看到文本在字符 3072 的 VIM 中不再被识别为字符串,原因我无法弄清楚。这是一个错误,还是我不熟悉的隐藏角色?文本复制在下面和屏幕截图 以显示文本突然从字符串变为常规文本。如何解决此问题以将此文本解析为字典?

{
'album_type': 'single',
'artists': [
{
'external_urls': {
'spotify': 'https://open.spotify.com/artist/4D75GcNG95ebPtNvoNVXhz'
},
'href': 'https://api.spotify.com/v1/artists/4D75GcNG95ebPtNvoNVXhz',
'id': '4D75GcNG95ebPtNvoNVXhz',
'name': 'Afrojack',
'type': 'artist',
'uri': 'spotify:artist:4D75GcNG95ebPtNvoNVXhz'
},
{
'external_urls': {
'spotify': 'https://open.spotify.com/artist/50kceDjknh0CjwOko7kx7V'
},
'href': 'https://api.spotify.com/v1/artists/50kceDjknh0CjwOko7kx7V',
'id': '50kceDjknh0CjwOko7kx7V',
'name': 'Jewelz & Sparks',
'type': 'artist',
'uri': 'spotify:artist:50kceDjknh0CjwOko7kx7V'
}
],
'available_markets': [
'AD',
'AR',
'AT',
'AU',
'BE',
'BG',
'BO',
'BR',
'CA',
'CH',
'CL',
'CO',
'CR',
'CY',
'CZ',
'DE',
'DK',
'DO',
'EC',
'EE',
'ES',
'FI',
'FR',
'GB',
'GR',
'GT',
'HK',
'HN',
'HU',
'ID',
'IE',
'IL',
'IS',
'IT',
'JP',
'LI',
'LT',
'LU',
'LV',
'MC',
'MT',
'MX',
'MY',
'NI',
'NL',
'NO',
'NZ',
'PA',
'PE',
'PH',
'PL',
'PT',
'PY',
'RO',
'SE',
'SG',
'SK',
'SV',
'TH',
'TR',
'TW',
'US',
'UY',
'VN',
'ZA'
],
'copyrights': [
{
'text': '2018 Wall Recordings under exclusive license to  Armada Music B.V.',
'type': 'C'
},
{
'text': '2018 Wall Recordings under  exclusive license to Armada Music B.V.',
'type': 'P'
}
],
'external_ids': {
'upc': '8718522203121'
},
'external_urls': {
'spotify': 'https://open.spotify.com/album/6ifJabSFs9iUQEDBdCmCz3'
},
'genres': [
],
'  href': 'https://api.spotify.com/v1/albums/6ifJabSFs9iUQEDBdCmCz3',
'id': '6ifJabSFs9iUQEDBdCmCz3',
'images': [
{
'height': 640,
'url': 'https://i.scdn.co/image/44ffb95971457076571b326619e8841163d50871',
'width': 640
},
{
'height': 300,
'url': 'https://i.scdn.co/image/4fe7ddaeade16d8aa1594450c69cb796a0045191',
'width': 300
},
{
'height': 64,
'url': 'https://i.scdn.co/image/d7d3f78ca3e308398085f12aac210e2f052d379e',
'width': 64
}
],
'label': 'Wall Recordings',
'name': 'One More Day',
'popularity': 55,
'release_date': '2018-05-04',
'release_date_precision': 'day',
'tracks': {
'href': 'https://api.spotify.com/v1/albums/6ifJabSFs9iUQEDBdCmCz3/tracks?  offset=0&limit=50',
'items': [
{
'artists': [
{
'external_urls': {
'spotify': 'https://open.spotify.com/artist/4D75GcNG95ebPtNvoNVXhz'
},
'href': 'https://api.spotify.com/v1/artists/4D75GcNG95ebPtNvoNVXhz',
'id': '4D75GcNG95ebPtNvoNVXhz',
'name': 'Afrojack',
'type': 'artist',
'uri': 'spotify:artist:4D75GcNG95ebPtNvoNVXhz'
},
{
'external_urls': {
'spotify': 'https://open.spotify.com/artist/50kceDjknh0CjwOko7kx7V'
},
'href': 'https://api.spotify.com/v1/artists/50kceDjknh0CjwOko7kx7V',
'id': '50kceDjknh0CjwOko7kx7V',
'name': 'Jewelz & Sparks',
'type': 'artist',
'uri': 'spotify:artist:50kceDjknh0CjwOko7kx7V'
}
],
'available_markets': [
'AD',
'AR',
'AT',
'AU',
'BE',
'BG',
'BO',
'BR',
'CA',
'CH',
'CL',
'CO',
'CR',
'CY',
'CZ',
'DE',
'DK',
'DO',
'EC',
'EE',
'ES',
'FI',
'FR',
'GB',
'GR',
'GT',
'HK',
'HN',
'HU',
'ID',
'IE',
'IL',
'IS',
'IT',
'JP',
'LI',
'LT',
'LU',
'LV',
'MC',
'MT',
'MX',
'MY',
'NI',
'NL',
'NO',
'NZ',
'PA',
'PE',
'PH',
'PL',
'PT',
'PY',
'RO',
'SE',
'SG',
'SK',
'SV',
'TH',
'TR',
'TW',
'US',
'UY',
'VN',
'ZA'
],
'disc_number': 1,
'duration_ms': 197645,
'explicit': True,
'external_urls': {
'spotify': 'https://open.spotify.com/track/3vPoCfS2Ao2pjOvKZ63ybx'
},
'href': 'https://api.spotify.com/v1/tracks/3vPoCfS2Ao2pjOvKZ63ybx',
'id': '3vPoCfS2Ao2pjOvKZ63ybx',
'is_local': False,
'name': 'One More Day',
'preview_url': 'https://p.scdn.co/mp3-  preview/f0f8d1144626032fcbabb2c22efcae3642e0a235?  cid=efb520d351794bbb8daa0d009fffcef4',
'track_number': 1,
'type': 'track',
'uri': 'spotify:track:3vPoCfS2Ao2pjOvKZ63ybx'
}
],
'limit': 50,
'next': None,
'offset': 0,
'previous': None,
'total': 1
},
'type': 'album',
'uri': 'spotify:album:6ifJabSFs9iUQEDBdCmCz3'
}

在解析之前,您应该将所有单引号替换为双引号。 它是JSON文件的标准

将所有单引号替换为双引号。

True应该是真的False应该是的,如果这是有意的,None应该用引号括起来或替换为null

使用ast 模块,我能够阅读您的字典。

import ast
with open(filename, "r") as infile:    
print(ast.literal_eval(infile.read()))

输出:

{'album_type': 'single', 'name': 'One More Day', 'uri': 'spotify:album:6ifJabSFs9iUQEDBdCmCz3', 'external_urls': {'spotify': 'https://open.spotify.com/album/6ifJabSFs9iUQEDBdCmCz3'}, 'release_date': '2018-05-04', 'popularity': 55, 'genres': [], 'label': 'Wall Recordings', '  href': 'https://api.spotify.com/v1/albums/6ifJabSFs9iUQEDBdCmCz3', 'tracks': {'items': [{'is_local': False, 'name': 'One More Day', 'external_urls': {'spotify': 'https://open.spotify.com/track/3vPoCfS2Ao2pjOvKZ63ybx'}, 'uri': 'spotify:track:3vPoCfS2Ao2pjOvKZ63ybx', 'explicit': True, 'preview_url': 'https://p.scdn.co/mp3-  preview/f0f8d1144626032fcbabb2c22efcae3642e0a235?  cid=efb520d351794bbb8daa0d009fffcef4', 'track_number': 1, 'disc_number': 1, 'href': 'https://api.spotify.com/v1/tracks/3vPoCfS2Ao2pjOvKZ63ybx', 'artists': [{'name': 'Afrojack', 'external_urls': {'spotify': 'https://open.spotify.com/artist/4D75GcNG95ebPtNvoNVXhz'}, 'uri': 'spotify:artist:4D75GcNG95ebPtNvoNVXhz', 'href': 'https://api.spotify.com/v1/artists/4D75GcNG95ebPtNvoNVXhz', 'type': 'artist', 'id': '4D75GcNG95ebPtNvoNVXhz'}, {'name': 'Jewelz & Sparks', 'external_urls': {'spotify': 'https://open.spotify.com/artist/50kceDjknh0CjwOko7kx7V'}, 'uri': 'spotify:artist:50kceDjknh0CjwOko7kx7V', 'href': 'https://api.spotify.com/v1/artists/50kceDjknh0CjwOko7kx7V', 'type': 'artist', 'id': '50kceDjknh0CjwOko7kx7V'}], 'duration_ms': 197645, 'type': 'track', 'id': '3vPoCfS2Ao2pjOvKZ63ybx', 'available_markets': ['AD', 'AR', 'AT', 'AU', 'BE', 'BG', 'BO', 'BR', 'CA', 'CH', 'CL', 'CO', 'CR', 'CY', 'CZ', 'DE', 'DK', 'DO', 'EC', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'GT', 'HK', 'HN', 'HU', 'ID', 'IE', 'IL', 'IS', 'IT', 'JP', 'LI', 'LT', 'LU', 'LV', 'MC', 'MT', 'MX', 'MY', 'NI', 'NL', 'NO', 'NZ', 'PA', 'PE', 'PH', 'PL', 'PT', 'PY', 'RO', 'SE', 'SG', 'SK', 'SV', 'TH', 'TR', 'TW', 'US', 'UY', 'VN', 'ZA']}], 'next': None, 'href': 'https://api.spotify.com/v1/albums/6ifJabSFs9iUQEDBdCmCz3/tracks?  offset=0&limit=50', 'limit': 50, 'offset': 0, 'total': 1, 'previous': None}, 'copyrights': [{'text': '2018 Wall Recordings under exclusive license to  Armada Music B.V.', 'type': 'C'}, {'text': '2018 Wall Recordings under  exclusive license to Armada Music B.V.', 'type': 'P'}], 'artists': [{'name': 'Afrojack', 'external_urls': {'spotify': 'https://open.spotify.com/artist/4D75GcNG95ebPtNvoNVXhz'}, 'uri': 'spotify:artist:4D75GcNG95ebPtNvoNVXhz', 'href': 'https://api.spotify.com/v1/artists/4D75GcNG95ebPtNvoNVXhz', 'type': 'artist', 'id': '4D75GcNG95ebPtNvoNVXhz'}, {'name': 'Jewelz & Sparks', 'external_urls': {'spotify': 'https://open.spotify.com/artist/50kceDjknh0CjwOko7kx7V'}, 'uri': 'spotify:artist:50kceDjknh0CjwOko7kx7V', 'href': 'https://api.spotify.com/v1/artists/50kceDjknh0CjwOko7kx7V', 'type': 'artist', 'id': '50kceDjknh0CjwOko7kx7V'}], 'images': [{'url': 'https://i.scdn.co/image/44ffb95971457076571b326619e8841163d50871', 'width': 640, 'height': 640}, {'url': 'https://i.scdn.co/image/4fe7ddaeade16d8aa1594450c69cb796a0045191', 'width': 300, 'height': 300}, {'url': 'https://i.scdn.co/image/d7d3f78ca3e308398085f12aac210e2f052d379e', 'width': 64, 'height': 64}], 'external_ids': {'upc': '8718522203121'}, 'type': 'album', 'id': '6ifJabSFs9iUQEDBdCmCz3', 'available_markets': ['AD', 'AR', 'AT', 'AU', 'BE', 'BG', 'BO', 'BR', 'CA', 'CH', 'CL', 'CO', 'CR', 'CY', 'CZ', 'DE', 'DK', 'DO', 'EC', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'GT', 'HK', 'HN', 'HU', 'ID', 'IE', 'IL', 'IS', 'IT', 'JP', 'LI', 'LT', 'LU', 'LV', 'MC', 'MT', 'MX', 'MY', 'NI', 'NL', 'NO', 'NZ', 'PA', 'PE', 'PH', 'PL', 'PT', 'PY', 'RO', 'SE', 'SG', 'SK', 'SV', 'TH', 'TR', 'TW', 'US', 'UY', 'VN', 'ZA'], 'release_date_precision': 'day'}

最新更新