首页 > 解决方案 > 查找单词是否包含在Python中带有路径的字典中

问题描述

我有一个 txt,每行包含这样的句子:

INT_ASTIK_EAS
ER_ASTOM_ASTIK
and so on.

我想要求一个主文件夹的目录,其中包含每个包含的子文件夹ASTIK.shp,然后是EAS.shp,然后是ASTOM.shp

INT表示与此代码执行交集,在这种情况下应该涉及ASTIKEASshapefile。

inte_s=gpd.overlay(ASTIK,EAS,how='intersection')

编码:

rootdir = r'C:\Users\user\Desktop\a' # path to your root directory you walk
sfiles = {} # a dictionary with all the .shp files
for entry in os.listdir(rootdir):
    dirpath = os.path.join(rootdir, entry)
    if os.path.isdir(dirpath): 
        for file in os.listdir(dirpath): # Get all files in the subdirectories
            if file.endswith('.shp'): # If it's an .shp.
                filepath = os.path.join(dirpath, file)
                sfiles[filepath] = gpd.read_file(filepath)


action_dict = {'INT': 'intersection', 'ER': 'difference'}


directory=input('Insert dir of the main folder')
with open(input()) as txtfile: #insert directory of txt
    x = txtfile.readlines()
for line in x:
    action, shape1, shape2 = line.split('_')  # here line is ER_ASTOM_ASTIK or whatever line in the txt file
    if shape1 in sfiles and shape2 in sfiles:
        gpd.overlay(sfiles[shape1], sfiles[shape2], how=action_dict[action])

它可能不会执行交集或差异。我如何将其结果存储在变量中,因为我必须对每行的每个操作的结果进行进一步处理。

在检查了sfiles变量的问题及其存储的内容之后:它给出了:

for x in sfiles:
    print (x)

C:\Users\user\Desktop\a\ASTTOM\ASTTOM.shp
C:\Users\user\Desktop\a\PST\PST.shp

最后:

if shape1 in sfiles:
    print('success')
else:
    print('failed')

failed

这意味着它不会在字典中传达它应该在字典中传达的内容,因为 shapefile 恰好存在于文件夹中。我怎样才能让它进行叠加计算?

print(sfiles)

{'C:\\Users\\user\\Desktop\\a\\ASTTOM\\ASTTOM.shp':      OBJECTID  FID_Asttom TheOTA_Tmp  aa    SHAPE_Leng    SHAPE_Area  \
0           1           1      20008   7   4206.346569  5.796467e+05   
1           2           2      20008  11   3311.521073  6.892735e+05   
2           3           3      20008   6   2131.153007  2.196689e+05   
3           4           4      20008   2   1738.249605  4.731495e+04   
4           5           5      20008   1   1075.903789  5.337986e+04   
5           6           6      20008  10   3255.422426  3.628100e+05   
6           7           7      20008   3   2469.427102  2.267493e+05   
7           8           8      20008   5   2466.593786  2.042149e+05   
8           9           9      20008   8   3095.473108  5.384131e+05   
9          10          10      20008   4   1856.659243  1.136976e+05   
10         11          11      20008   9   2293.865083  2.063858e+05   
11         12          12      20104   4   4445.338873  4.455547e+05   
12         13          13      20104   7   3659.621424  2.834771e+05   
13         14          14      20104   2   2098.126160  1.981298e+05   

它打印字典中所有 shp 的属性表

如果尝试这样做:

print(sfiles[shape1], sfiles[shape2], action_dict[action])

KeyError                                  Traceback (most recent call last)
<ipython-input-11-9f435f40293e> in <module>()
----> 1 print(sfiles[shape1], sfiles[shape2], action_dict[action])

KeyError: 'ASTTOM'


and each every time:
print(sfiles[shape1])
KeyError: 'ASTTOM'

print(sfiles[shape2])
KeyError: 'PST'

print(action_dict[action])
difference

基本上:有一个带有这样键的字典:'C:\\Users\\user\\Desktop\\a\\ASTTOM\\ASTTOM.shp'当提供键'ASTTOM'时,这就是为什么返回错误。我该怎么办?阅读txt是最好的程序吗?

标签: python

解决方案


推荐阅读