Home > database >  python - TypeError: object of type 'zip' has no len()
python - TypeError: object of type 'zip' has no len()

Time:03-11

I have a GeoJSON file with many features in it and it is in the following format:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "fid": 109,
                "unique_property_number": 787387.0,
                "unique_building_number": 7627864.0,
                "property_area": 91.5,
                "building_area": 91.5,
                "mapping_block_number": "FH0801",
                "height": 5.8,
                "age": "MODERN",
                "use": "UTILITIES",
                "data_level": "FHAU",
                "data_link": null,
                "join_on": null
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            -335013.4478176794,
                            7597020.21467809
                        ],
                        [
                            -335034.52685820695,
                            7597002.6574345445
                        ],
                        [
                            -335041.3940838162,
                            7597010.936560697
                        ],
                        [
                            -335020.31503652286,
                            7597028.493842001
                        ],
                        [
                            -335013.4478176794,
                            7597020.21467809
                        ]
                    ]
                ]
            }
        },       
etc

But I am trying to add a name field within the properties of each of the features by using this python script:

import json

with open('C:/inputfile.json', 'r') as f:
    data = json.load(f)

#A Python dictionary containing properties to be added to each GeoJSON Feature
properties_dict={
    "name": "Building"
    }
#Convert the dictionary to a list
properties_list=zip(properties_dict.keys(),properties_dict.values())

#Loop over GeoJSON features and add the new properties
for feat in data['features']:
    for i in range(len(properties_list)):
        feat ['properties'][properties_list[i][0]]=properties_list[i][1]

#Write result to a new file
with open('C:/outputfile.json', 'w') as f:
    json.dump(data, f)

However I am getting the below error (full traceback now included):

Traceback (most recent call last):
  File "c:\Users\Public\import os1.py", line 15, in <module>
    for i in range(len(properties_list)):
TypeError: object of type 'zip' has no len()

Can anyone help me pass this error message?

CodePudding user response:

This is one of the reasons python discourages this pattern:

for i in range(len(properties_list)):

You don't need to do this. zip objects are iterable, which means you can iterate directly over them:

properties_list=zip(properties_dict.keys(),properties_dict.values())

for feat in data['features']:
    for pair in properties_list:

Further, you don't need zip at all. You are recreating a dict method that already exists: .items() which is also iterable and allows the much simpler:

properties_dict={"name": "Building"}

for feat in data['features']:
    for key, value in properties_dict.items():
        feat['properties'][key]=value

CodePudding user response:

zip doesn't return a list; it returns a lazy iterable. If you want a list, you need to force it into a list manually:

properties_list = list(zip(properties_dict.keys(),properties_dict.values()))
  • Related