Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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从逗号分隔的值中动态选择值_Python - Fatal编程技术网

Python从逗号分隔的值中动态选择值

Python从逗号分隔的值中动态选择值,python,Python,假设我有以下字符串: University of example1 Assistent professor, hello, University of example2 Hello, University of example3 如何仅检索包含“University”的值,以便输出如下 University of example1 University of example2 University of example3 取每个字符串,用逗号分隔,然后检查每个片段是否为“大学” 您可以将字符

假设我有以下字符串:

University of example1
Assistent professor, hello, University of example2
Hello, University of example3
如何仅检索包含“University”的值,以便输出如下

University of example1
University of example2
University of example3

取每个字符串,用逗号分隔,然后检查每个片段是否为“大学”


您可以将字符串转换为数组,然后使用或列表理解来筛选出不需要的字符串

类似于以下的方法应该可以工作:

# This will probably come from your file IRL
# We want a list of strings that we can split later and parse
plaintext = """University of example1
Assistent professor, hello, University of example2
Hello, University of example3"""
lines = plaintext.splitlines()


# Define a function to pass into filter
# You'll want to change this to taste, maybe use a regexp depending on requirements
def is_uni(text):

    # Strip out any leading spaces
    return text.lstrip().startswith("Uni")

for line in lines:
    for uni in filter(is_uni,line.split(',')):
        print uni

按照您的意愿使用
有效的\u字符串。

XPath用于选择XML文档的某些部分。为什么要在没有XML的情况下使用XPath?是的,我使用XML,但我正忙于Python scrapy中的一个项目。所以我刮取了我的XML并放入了一个数组。但后来我发现数组中的一些值仍然没有用逗号分隔。但是,我只需要在数组中包含大学值。如果不可能在xml中完成这样的事情,我将在python中寻找其他选项。如果您想使用XPath,请发布XML;否则,请用Python重写并重新标记您的问题。
# This will probably come from your file IRL
# We want a list of strings that we can split later and parse
plaintext = """University of example1
Assistent professor, hello, University of example2
Hello, University of example3"""
lines = plaintext.splitlines()


# Define a function to pass into filter
# You'll want to change this to taste, maybe use a regexp depending on requirements
def is_uni(text):

    # Strip out any leading spaces
    return text.lstrip().startswith("Uni")

for line in lines:
    for uni in filter(is_uni,line.split(',')):
        print uni
data_string = "University of example1
Assistent professor, hello, University of example2
Hello, University of example3"
valid_strings = []
strings = data_string.split(",")
for string in strings:
    if "University" in string:
        valid_strings.append(string)