[英]How to Remove those keywords that not belongs to City from a Dictionary in python Text file?
我有Dictionary.txt
城市名称字典文件并匹配 CSV 文件中的城市名称并计算每一行的匹配数。
我的Dictionary.txt
文件有问题,它有一些不属于城市名称的关键字。 所以我想从字典文件中删除所有这些不相关的关键字。 我不知道如何解决它。
例如 Dictionary.txt 的一些关键字是:
Nowy Dworek
Dar Bel Amri
Abaren
Hassi blal
Ambodivona
Chakla
Ippatam
Suti
Via
Zingeyskiy
Luesslingen
Bolshaya Markha
Ard Na Greine
Raskhovets
Ksizovo
Rock Elm
Batnahit
在这个文件中,我有很多不相关的关键字,例如,在给定的样本中, via
关键字不属于城市,与下面给出的输出结果相同,您可以看到有许多不相关的关键字在描述中匹配,
Sr_Num | Description Cities |matched Keywords |Cities Total matches
1 | any description........|temple , via , Thai |3
2 last , canada , give , on| 4
3 this , is , on , louis |4
4 Ocean , I , US , a , is , Southern , huge , of , this , War| 10
5 queen |1
6 But , is , me , cole| 4
7 all , Lester , Mason , is , on , us , long , of| 8
8 Wallach , Bad , Good , Sanchez |4
那么,从不属于城市名称的dictionary.txt
文件中删除所有不相关关键字的解决方案是什么???
我不会提供代码,因为我认为你可以自己做,但这是我的做法:
首先,拆分您的Dictionary.txt
以将所有单词放入一个列表中。
然后,拆分您的 CSV 文件以将每个城市名称放入一个列表中。
然后,循环遍历最后一个列表以检查它是否是您的词典列表中的单词,如果不是,则将其从列表中删除。
最后,从你得到的最终列表中重写你的 CSV。
编辑:这里有一些代码向您展示:
yourDictionnary = open('Dictionary.txt', 'r').read().splitlines() #this puts contents from the dictionnary into a list line by line
theCsvContent = ','.join(open('csvName.csv', 'r').read().splitlines()).split(',') #this puts contents from the csv into a list element by element
for index, word in enumerate(theCsvContent): #loops through theCsv with index as the word index and word as the word we're iterating on
if word not in yourDictionnary: #checks if the word is in dictionnary and if not :
del theCsvContent[index] #removes the word from the csv
open('result.csv', 'w').write(','.join(theCsvContent)) #this writes the edited csv into result.csv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.