在Python中打印JSON文件中的特定行

时间:2018-06-10 17:58:26

标签: python json

所以我将一个JSON文件作为列表上传到python,如下所示:

import pandas as pd
import json

with open('data.json') as json_file:      
    json_file = json_file.readlines()
    json_file = list(map(json.loads, json_file))

我想返回包含不包含数字的地址的所有行(返回整行)。我写的代码是这样的,但我一直收到错误。 (我用过try,除非因为有些行没有地址而且我不希望代码跳过它们):

for i in range (0, len(json_file)):
    try: 
        for line in json_file:
            add = json_file[i]['payload']['address']
            addresses = add.split(" ")
        try:
            address = int(addresses[0])
            if type(address) =! int: 
                print(line)

        except: 
            continue

    except:
        continue

作为参考,这是一个json在我正在使用的文件中的样子:

{
"payload": {
    "existence_full": 1,
    "geo_virtual": [
        "50.794876|-1.090893|20|within_50m|4"
    ],
    "latitude": "50.794876",
    "locality": "Portsmouth",
    "_records_touched": {
        "crawl": 16,
        "lssi": 0,
        "polygon_centroid": 0,
        "geocoder": 0,
        "user_submission": 0,
        "tdc": 0,
        "gov": 0
    },
    "email": "info.centre@port.ac.uk",
    "existence_ml": 0.9794948816203205,
    "address": "Winston Churchill Av",
    "longitude": "-1.090893",
    "domain_aggregate": "",
    "name": "University of Portsmouth",
    "search_tags": [
        "The University of Portsmouth",
        "The University of Portsmouth Students Union",
        "University House"
    ],
    "admin_region": "England",
    "existence": 1,
    "post_town": "Portsmouth",
    "category_labels": [
        [
            "Community and Government",
            "Education",
            "Colleges and Universities"
        ]
    ],
    "region": "Hampshire",
    "review_count": "1",
    "geocode_level": "within_50m",
    "tel": "023 9284 8484",
    "placerank": 42,
    "placerank_ml": 69.2774043602657,
    "address_extended": "Unit 4",
    "category_ids_text_search": "",
    "fax": "023 9284 3122",
    "website": "http: //www.port.ac.uk",
    "status": "1",
    "neighborhood": [
        "The Waterfront"
    ],
    "geocode_confidence": "20",
    "postcode": "PO1 2UP",
    "category_ids": [
        29
    ],
    "country": "gb",
    "_geocode_quality": "4"
},
"uuid": "297fa2bf-7915-4252-9a55-96a0d44e358e"
}

0 个答案:

没有答案