从文本文件(Python)解析/循环JSON对象 [英] Parsing/Looping over JSON objects from a text file (Python)

查看:680
本文介绍了从文本文件(Python)解析/循环JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题.我有一个文本文件,其中包含保存房地产信息的JSON对象,如下所示:

simple question. I have a text file containing JSON objects which hold real estate information, like so:

     {
      "marketing_package_url": "http://www.capitalpacific.com/inquiry/TrailsEndMarketplaceExecSummary.pdf",
     "title": "TRAILS END MARKETPLACE",
      "location": "OREGON CITY, OR"
      }

在我的脚本中,我有一个营销软件包URL的列表.我想将该文本文件中json对象中的URL与该列表中的URL进行交叉检查,以便找到新" URL.有什么建议?我一直在考虑将json对象导入一个单独的列表,然后使用该列表中的URL.

In my script, I have a list of marketing package URLS. I want to cross-check the URLS from that list with the URL's in the json objects in this text file so I can find the 'new' URLS. Any suggestions? I was thinking along the lines of importing the json objects into a separate list, and then using the URL's from that list.

import json
with open('properties.txt', 'r') as filename:
           data = json.loads('properties.txt')

如何将其拆分并将URL发送到列表?

how to split it up and send URLS to a list?

推荐答案

如果数据是字典列表,则可以使用以下代码获取列出的marketing_package_urls列表.

You can use the following code to get the list of marketing_package_urls to list, if the data is a list of dictionaries.

list = [x['marketing_package_url'] for x in data]

这篇关于从文本文件(Python)解析/循环JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆