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”。

data = """University of example1
Assistent professor, hello, University of example2
Hello, University of example3"""

data = data.replace("\n",",")   #All one line, split with commas
slices = data.split(",")        #Get each slice
for slice in slices:            #Go through each slice
    if "University" in slice:   #check for magic word
        print slice.strip()     #print out what matches, remove trailing and leading white space

关于Python 从逗号分隔值中动态选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30791128/

相关文章:

python - hash() 如何计算元组的哈希值?

python - 使用 Python CSV 模块读取带有 BOM 的 UTF-8 会导致不需要的额外字符

python - 如何使 matplotlib/pylab 中的 X Axis 不自动对值进行排序?

python时间间隔重叠持续时间

python - 使用 PySide 的更多停靠位置?

Python优化如何在列表中查找重复值和值索引

python - 错误 : torch is not a supported wheel on this platform (Linux, 非 conda )

python - 使用 Python RegEx re.findall 解析文本

python - "col_level"在melt函数中起什么作用?

python - 当身份验证类型为 Azure Active Directory 时,通过 Python 连接到 sql-server