从json字典中提取组件

时间:2017-12-08 12:18:49

标签: python json list

我有一个json字典(result_dict_tag),如下所示:

{'10644906845': {'photo': {'id': '10644906845',
   'tags': {'tag': [{'_content': 'srilanka',
      'author': '34665545@N05',
      'authorname': 'CrishyM',
      'id': '34660205-10644906845-23098',
      'machine_tag': 0,
      'raw': 'sri lanka'},
     {'_content': 'navy',
      'author': '34665545@N05',
      'authorname': 'CrishyM',
      'id': '34660205-10644906845-7137',
      'machine_tag': 0,
      'raw': 'navy'},
     {'_content': 'vessel',
      'author': '34665545@N05',
      'authorname': 'CrishyM',
      'id': '34660205-10644906845-8792',
      'machine_tag': 0,
      'raw': 'vessel'}]}},
  'stat': 'ok'},
      '11778819726': {'photo': {'id': '11778819726',
   'tags': {'tag': [{'_content': 'sri',
      'author': '44455081@N07',
      'authorname': 'Vinchel',
      'id': '44433751-11778819726-12165',
      'machine_tag': 0,
      'raw': 'sri'},
     {'_content': 'lanka',
      'author': '44455081@N07',
      'authorname': 'Vinchel',
      'id': '44433751-11778819726-12166',
      'machine_tag': 0,
      'raw': 'lanka'},
     {'_content': 'mirissa',
      'author': '44455081@N07',
      'authorname': 'Vinchel',
      'id': '44433751-11778819726-1158107',
      'machine_tag': 0,
      'raw': 'mirissa'}]}},
  'stat': 'ok'}}

我想要的是提取原始的'每个字典中的单词(例如result_dict_tag['10644906845']['photo']["tags"]['tag'][0]['raw'])并创建一个包含用户ID和每个用户相关原始单词的数据框。我觉得这需要一些for循环。我写了一篇,它只会导致“原始”的结果。仅与第一个用户相关的单词。我已经阅读了类似的文档和各种问题。但无法弄明白。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

您可以在单个列表理解中提取这些

raw_words = [mydict[i]['photo']["tags"]['tag'][k]['raw']
             for k in range(len(mydict[i]['photo']["tags"]['tag']))
             for i in mydict]

print(raw_words)
>> ['sri', 'sri lanka', 'lanka', 'navy', 'mirissa', 'vessel']

答案 1 :(得分:0)

如果我理解正确,您想要的是list dicts dict,每个id包含rownew_list = result_dict_tag['10644906845']['photo']["tags"]['tag'] new_desired_list = [] for index in range(len(new_list)): tempDict = {} for key in new_list[index]: if(key=='id'): tempDict['id'] = new_list[index][key] if(key=='raw'): tempDict['row'] = new_list[index][key] new_desired_list.append(tempDict) print(new_desired_list)

在你的词典下添加以下代码行:

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>facebook login test</title>
	<script src="/Vendors/jQuery/jquery-3.2.1.min.js"></script>
</head>
	<body>
		<script>
		window.fbAsyncInit = function() {
			FB.init({
				appId      : 'foobar123456', // see https://developers.facebook.com/apps/
				cookie     : true,
				xfbml      : true,
				version    : 'v2.11' // see https://developers.facebook.com/docs/javascript/quickstart
			});
			
			FB.AppEvents.logPageView();   
			
		};

		(function(d, s, id){
			var js, fjs = d.getElementsByTagName(s)[0];
			if (d.getElementById(id)) {return;}
			js = d.createElement(s); js.id = id;
			js.src = "https://connect.facebook.net/en_US/sdk.js";
			fjs.parentNode.insertBefore(js, fjs);
		}(document, 'script', 'facebook-jssdk'));
		</script>

		<!-- LOGIN BUTTON scope at: https://developers.facebook.com/docs/facebook-login/permissions/ -->
		<div class="fb-login-button" data-width="250" data-max-rows="1" data-size="large" onlogin="fbLoginHandler" data-scope="public_profile,email" data-button-type="login_with" data-show-faces="false" data-auto-logout-link="true" data-use-continue-as="true"></div>

		<script>
			console.log("reached script tag");
			var timesfbLoginHandlerFired = 0;
			var fbLoginHandler = ()=>{
				timesfbLoginHandlerFired++;
				console.log("fbLoginHandler fired ["+timesfbLoginHandlerFired+"] times");
			};
		</script>
	</body>
</html>