仅需根据搜索返回一个单元格 [英] Need to return only one cell based on the search

查看:72
本文介绍了仅需根据搜索返回一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在python中进行麦芽麦转换,我需要根据此搜索仅返回一个单元格

I am trying a maltego transform in python, what I need is to return only one cell based on this search

@staticmethod
   def get_names(search_person_name):
    matching_names = []
    with open("examplefile.csv") as f:
        for ln in f.readlines():
            person_name, data = ln.split(",", 1)
            if person_name.strip() == search_person_name.strip():
                matching_names.append(data.strip())
    return matching_names

.csv文件由以下列组成:

the .csv file consists of the following columns:

person_name,data,streetaddress

根据搜索条件,我只希望返回单元格数据

I would like, based on the search criteria it would only return the cell data

包含 streetaddress列

有什么想法吗?

推荐答案

为什么您的数据值是数据,街道地址;是因为您将拆分功能设置为仅执行1个拆分。

The reason why your "data" value was "data,streetaddress" was because you set your split function to only perform 1 split.

person_name, data = ln.split(",", 1)

split()函数的第二个参数设置最大分割数。由于您将其设置为1,因此它只会拆分第一个,它所看到的。由于文件的内容是 person_name,data,streetaddress,因此变量变为:

The second argument of the split() function sets the max number of splits. Since you set it at 1, it only split the first "," that it saw. Since the content of the file is "person_name,data,streetaddress", then the variables became:

person_name = person_name
data = data,streetaddress

最简单的解决方法,如@TBaggins所提到的,是将该行更改为:

The easiest fix, as mentioned by @TBaggins is to change that line into:

person_name, data, streetaddress = ln.split(",", 2)

这会从行中进行2个分割,并将这3个值放入各自的变量中。

This makes the 2 splits from the line and places those 3 values in their respective variables.

这篇关于仅需根据搜索返回一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆