Getting the attributes' values
Let's explore the attributes of the world borders to find out why we were unable to get the names.
Edit the
if __name__ == '__main__':
block:if __name__ == '__main__': world = BoundaryCollection("../data/world_borders_simple.shp") print(world.data[0].attributes.keys())
Run the code and look at the output:
File imported: ../data/world_borders_simple.shp ['SUBREGION', 'POP2005', 'REGION', 'ISO3', 'ISO2', 'FIPS', 'UN', 'NAME'] Process finished with exit code 0
What we did was we got the first item in
world.data
and then printed its attribute keys. The list shown in the output has aNAME
key, but it is all in the uppercase. This is very common for Shapefiles whose data is contained in the DBF files.Since we don't want to worry if the attributes' names are in the uppercase or lowercase, we have two possible solutions: convert the names at the moment of the import or convert the names on the fly when the attribute value is requested. Depending on your application...