【问题标题】:reading shp files to geopandas to dictionary with the same name将 shp 文件读取到 geopandas 到同名字典
【发布时间】:2020-10-19 19:01:55
【问题描述】:

我正在浏览一个目录结构,查找所有扩展名为 .shp 的文件并将它们存储在字典中。但是,有些文件名称相同,如何将同名文件存储在字典中而不被覆盖?在这种情况下,将文件结构附加到名称是可以接受的。这是怎么做到的?

当前“工作”代码:

def get_all_shp(mydir):
    # layers = []
    data = {}
    for root, dirs, files in os.walk(mydir):
        for file in files:
            try:
                if file.endswith(".shp"):
                    shp = os.path.join(root, file)
                    # layers.append(shp)
                    path = root + "/" + file
                    # print("path: " + path)
                    data[file] = gpd.read_file(path)

            except:
                pass

【问题讨论】:

  • 我已经尝试过 geopands.Dataframe.rename() 但据我所知这是用于列重命名?
  • 这解决了问题data[path] = gpd.read_file(path)

标签: python-3.x dictionary geopandas


【解决方案1】:
def get_all_shp(mydir):
    # layers = []
    data = {}
    for root, dirs, files in os.walk(mydir):
        for file in files:
            try:
                if file.endswith(".shp"):
                    shp = os.path.join(root, file)
                    # layers.append(shp)
                    path = root + "/" + file
                    # print("path: " + path)
                    data[path] = gpd.read_file(path)
            except:
                pass

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    相关资源
    最近更新 更多