0

I am trying to export multiple gdf to shp and name these shp by the gdf name. I have found this solution

here is my list:

maloobchod_list = [maloobchod2006_geom, maloobchod2011_geom, maloobchod2015_geom, maloobchod2016_geom]

and here is the code:

folderpath=r"E:\folder"
shp = shp

for maloobchod, gdf in enumerate(maloobchod_list, 1):
    filename="gdf_{}.{}".format(maloobchod, shp)
    filepath=os.path.join(folderpath, filename)
    gdf.to_file(filepath, encoding='utf-8')

It looks like it saves all the shp from the list, but the names are not the same as the actual gdf from the list.

1 Answer 1

1

There's a few issues with your code:

  • shp = shp This is incorrect, either use shp = "shp" of just use ".shp" in the code (as per below)
  • maloobchod is the loop enumeration value (i.e. 1, 2, etc...), not the name of the gdf. And for what you want, you don't need to use enumerate.
  • You can't access the name of the original list element without some seriously hacky code, just don't. Use a dict instead.

E.g.

maloobchod_dict = {"maloobchod2006": maloobchod2006_geom, "maloobchod2011": maloobchod2011_geom, "maloobchod2015": maloobchod2015_geom, "maloobchod2016": maloobchod2016_geom}
folderpath=r"E:\folder"

for gdf_name, gdf in maloobchod_dict.items():
    filename="gdf_{}.shp".format(gdf_name)
    filepath=os.path.join(folderpath, filename)
    gdf.to_file(filepath, encoding='utf-8')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.