将所有标签存储在艺术家[i] ['tags']中

时间:2017-11-18 22:42:44

标签: python json

我有以下代码来获取最后一个fm api的艺术家标签(流派)。每位艺术家都可以拥有许多标签,如:

function SaveAjax(dataURL){
  window.setInterval(function(){
    $.ajax({
      type: "POST",
      url: "saveImage.php",
      data: { 
         imgBase64: dataURL
      }
    }).done(function(o) {
      // console.log('saved');
    });
  })
}

function loadFile(f) {
    var reader = new FileReader();
    reader.onloadend = function () {
        var arte = new Image();
        arte.crossOrigin = "anonymous";
        arte.onload = function(){
            context.drawImage(arte, 0, 0, 1200, 1200)
            var dataURL = canvas.toDataURL("image/png");
            SaveAjax(dataURL);
        }
        arte.src = reader.result;

        // images.push({
        //     name : f.name, // use whatever naming magic you prefer here
        //     image : reader.result
        // });
    };
    reader.readAsDataURL(f);
}

$(":file").change(function(event) {
    var files = this.files;

    for (var i = 0; i < files.length; i++) {
        loadFile(files[i]);
    }
})

我想在 {'artist': {'tags': {'tag': [{'name': 'alternative', 'url': 'https://www.last.fm/tag/alternative'}, {'name': 'indie', 'url': 'https://www.last.fm/tag/indie'}, {'name': 'electronic', 'url': 'https://www.last.fm/tag/electronic'}]}, 中存储艺术家的所有标签。问题是我只是用这段代码得到了最后一个标签:

artists[i]['tags']

更多相关代码:

for artist in artist_data['artist']['tags']["tag"]:
    tags = artist["name"]
    artists[i]['tags'] = tags
print(artists[i])

您知道如何在artists = {} for i,v in artists.items(): chosen = artists[i]['name'].replace(" ", "+") artist_response = requests.get('http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&format=json&artist='+chosen+'&api_key='+api_key) artist_data = artist_response.json() for artist in artist_data['artist']['tags']["tag"]: tags = artist["name"] artists[i]['tags'] = tags print(artists[i])

中存储所有代码

1 个答案:

答案 0 :(得分:1)

您在每个循环中用新artists[i]['tags']替换tags 您可能希望像这样附加到它:

artists[i]['tags'].append(tags)

您必须在循环前创建artists[i]['tags'] = []