Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中的csv文件中查找匹配字符串?_Python_Python 3.x_String - Fatal编程技术网

如何在Python中的csv文件中查找匹配字符串?

如何在Python中的csv文件中查找匹配字符串?,python,python-3.x,string,Python,Python 3.x,String,我有一个程序,可以计算一系列气象站的前三个最大值。排名前三位的气象站名称是一个字符串,由排序列表(从最大到最小)“maxstat”确定,如下所示: txtmax = '\n{}\n{}\n{}'.format(maxstat[0], maxstat[1], maxstat[2]) 例如,其中txtmax可以是 'Baal' 'Arnhem' 'Arendonk'

我有一个程序,可以计算一系列气象站的前三个最大值。排名前三位的气象站名称是一个字符串,由排序列表(从最大到最小)“maxstat”确定,如下所示:

txtmax = '\n{}\n{}\n{}'.format(maxstat[0],
                               maxstat[1],
                               maxstat[2])
例如,其中txtmax可以是

'Baal'
'Arnhem'   
'Arendonk'  
现在我有了另一个用Python导入的csv文件,其中列出了站点所在的国家(按字母顺序排列):

countrystationsfile = os.path.join(
    os.path.dirname(__file__),
    'resources', 'stations_country.csv')
countrystations = pd.read_csv(countrystationsfile)
其中文件看起来像:

如何使txtmax自动在CountryStationFile中搜索站点(列位置)的相应匹配字符串,从而确定其国家(列国家)?最后,我希望txtmax有以下内容:

'Baal, BE'
'Arnhem, NL'   
'Arendonk, BE'  

已经谢谢你了

如果数据框中的条目是唯一的,您可以从这两列创建一个字典,然后使用该字典在国家/地区进行映射,例如:

d = dict(zip(countrystations['Place'], countrystations['Country']))
然后:

txtmax = '\n{}, {}\n{}, {}\n{}, {}'.format(maxstat[0], d[maxstat[0]],
                                           maxstat[1], d[maxstat[1]],
                                           maxstat[2], d[maxstat[2]])

以格式化文本的形式发布CSV文件的简短片段。数据的图像没有帮助。此外,你忘了显示你的“统计数据”,那么我们如何确定前三名呢?我把它作为文本放在图片下面。由maxstat确定的字符串以及txtmax列表中的站始终在变化,因为气象站的温度始终在变化。
txtmax = '\n{}, {}\n{}, {}\n{}, {}'.format(maxstat[0], d[maxstat[0]],
                                           maxstat[1], d[maxstat[1]],
                                           maxstat[2], d[maxstat[2]])