使用ast.literal\u eval或JSON解析字典时出错。写字符串时可能出现错误?python

使用ast.literal\u eval或JSON解析字典时出错。写字符串时可能出现错误?python,python,json,string,dictionary,spotify,Python,Json,String,Dictionary,Spotify,我试图在Python3中解析从Spotify Album API查询的词典,但收到错误消息: ValueError:节点或字符串格式错误 使用ast.literal\u eval时。使用JSON.loads时,我收到了错误消息: 语法错误:扫描字符串文字时下线 当查看字典时,我看到文本在VIM中的字符3072处不再被识别为字符串,原因我无法理解。这是一个bug,还是一个我不熟悉的隐藏角色?文本复制到下面,并添加一个 附加以显示文本从字符串突然变为常规文本。如何解决此问题以将此文本解析为字典

我试图在Python3中解析从Spotify Album API查询的词典,但收到错误消息:
ValueError:节点或字符串格式错误
使用
ast.literal\u eval
时。使用
JSON.loads
时,我收到了错误消息:
语法错误:扫描字符串文字时下线

当查看字典时,我看到文本在VIM中的字符3072处不再被识别为字符串,原因我无法理解。这是一个bug,还是一个我不熟悉的隐藏角色?文本复制到下面,并添加一个 附加以显示文本从字符串突然变为常规文本。如何解决此问题以将此文本解析为字典

   {
  '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应该是TrueFalse应该是FalseNone应该用引号括起来,或者用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'}

在上面的代码块中,字典看起来很好。因此,我不明白为什么我会犯这样的错误,或者截图中显示的奇怪效果。谢谢。我尝试了你的建议,收到了一个新错误,这让我怀疑问题是由与3072位置相关的内容引起的:json.decoder.jsondecoderror:Expecting value:line 1 column 3072(char 3071)为什么字典中的值不能是布尔值或非类型?我用双引号替换了所有单引号,但没有用。JSON中的布尔值是可能的。但是,在JSON中,它们被指定为true/false,第一个字母为小写:t和f。JSON.loads方法对JSON进行解码,因此您提供给它的输入应该是有效的JSON字符串。上面提供的代码不是有效的JSON字符串,而是有效的python对象层次结构。尝试使用JSON.dumps而不是JSON.loads,它将生成等效的JSON字符串输出。感谢您的澄清和帮助!!