以下是我想从中提取数据的JSON:
{
response: {
status: {
version: "4.2",
code: 0,
message: "Success"
},
songs: [
{
artist_id: "ARVNK831187FB48831",
artist_name: "Shakedown",
id: "SOTBATA14637546D06",
audio_summary: {
key: 0,
analysis_url: "http://echonest-analysis.s3.amazonaws.com/TR/0eGYPgCeySxSy0s0JlZ4qTFMSz7mKaf9KIHrvAYAUv19Htvo36Zdhf-LMzl8S5uCB8ilRysOkuK6iaVq0%3D/3/full.json?AWSAccessKeyId=AKIAJRDFEY23UEVW42BQ&Expires=1441915414&Signature=Lz0toHc9qPc7JhqQfw4GTKqub%2Bs%3D",
energy: 0.735735,
liveness: 0.16071,
tempo: 123.751,
speechiness: 0.041669,
acousticness: 0.001859,
instrumentalness: 0.854309,
mode: 1,
time_signature: 4,
duration: 304.22621,
loudness: -17.239,
audio_md5: "11265e013f2a0a4b1662802e79c0e60f",
valence: 0.416394,
danceability: 0.62834
},
title: "Shakedown"
},
{
artist_id: "ARFRPM21187FB463D4",
artist_name: "Stitch",
id: "SOEIDRQ13134388B5E",
audio_summary: {
key: 8,
analysis_url: "http://echonest-analysis.s3.amazonaws.com/TR/fg2_TwVsfQfw_0pj9zTypz_QDVHB6AtqG8fpEHpzp3m4dYOQ3WK_6F85sDSdjyAobbDnQ0skibPzvnCPc%3D/3/full.json?AWSAccessKeyId=AKIAJRDFEY23UEVW42BQ&Expires=1441915414&Signature=Ssn3gUXUrlQUX0AnT7koiRXxLog%3D",
energy: 0.556659,
liveness: 0.104145,
tempo: 170.154,
speechiness: 0.055633,
acousticness: 0.000003,
instrumentalness: 0.208791,
mode: 1,
time_signature: 4,
duration: 89.30621,
loudness: -15.669,
audio_md5: "ab37a2b1d55c05f21b9911823fd9d6e9",
valence: 0.430376,
danceability: 0.108284
},
title: "Stitch The Rich"
},
{
artist_id: "ARFRPM21187FB463D4",
artist_name: "Stitch",
id: "SOFQMJV137488CDC9C",
audio_summary: {
key: 10,
analysis_url: "http://echonest-analysis.s3.amazonaws.com/TR/H-wlTALk0i5K0cDtERTleqCQQ0-r77dh-RAm3A/3/full.json?AWSAccessKeyId=AKIAJRDFEY23UEVW42BQ&Expires=1441915414&Signature=5DnWlX0RrJUAsTm1WJUGbxYrjFk%3D",
energy: 0.535085,
liveness: 0.107299,
tempo: 168.191,
speechiness: 0.050538,
acousticness: 0.000002,
instrumentalness: 0.79286,
mode: 1,
time_signature: 1,
duration: 89.48925,
loudness: -16.347,
audio_md5: "1900df39bce419aeac5b1f2579f6b0a2",
valence: 0.504322,
danceability: 0.098206
},
title: "Stitch The Rich"
}
]
}
}
我想一次将数据拉到一个列表,并使用difflib将其与字符串进行比较。如果比率大于0.7,那么我想要使用它,否则我想丢弃它。以下是我编写的代码。它不会迭代超过一次。请帮忙
iter=1
for i in result['response']['songs']:
print("iteration #", iter)
iter=iter+1
artist=['artist_name']
print(artist)
song_name=['title']
print(song_name)
find=artist + ' ' + song_name
print(find)
seq=difflib.SequenceMatcher(find=find.lower(), line=line.lower())
similarity= seq.ratio()
print(similarity)
if similarity>0.7:
artist_final=artist
song_final=song_name
analysis_url=result['response']['songs'][iter]['audio_summary']['analysis_url']
print(analysis_url)
write_value= artist_final+ ' ' + song_final + ' ' + analysis_url
Append=open('Results.txt' , 'a')
Append.write(write_value + '\n'+ '\n')
Append.close
break
else:
print("trying next item")