假设我有以下字符串:
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/