python - 识别文本之间的字符串值

标签 python

我有以下 ['You and i','everyone else','cat and dog','We u all']

我需要以某种方式识别 andu 旁边的字符串。

例如,我期望以下输出:

You

i

cat

Dog

We

all

基本上,每个句子应该从andu 拆分出来。我需要打印 andu 两侧的两个文本。

我做的是错误的,但这是我的尝试之一:

sen = [w for w in words if re.search(r'.*and*.','.*u*.', w)]
for st in sen:
    print st

最佳答案

遍历每一行。检测它是否有 andu。如果是,则将其拆分到该 token 上并最终打印。对于所有其他行忽略。

>>> sentences = ['You and i', 'everyone else', 'cat and dog', 'We u all']
>>> for line in sentences:
...     if 'and' in line:
...         for split_word in line.split('and'):
...             print split_word.strip()
...     elif ' u ' in line:
...         for split_word in line.split(' u '):
...             print split_word.strip()
...     else:
...         pass
... 
You
i
cat
dog
We
all
>>> 

关于python - 识别文本之间的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17990936/

相关文章:

python - 在 Django 中打印对象

python - 在生产中训练机器学习

python - 如何为想要付费的人提供 'package' 一个简单的单文件 python 脚本?

python - Python 打印/格式化方法之间的区别

jquery - 使用 ajax 和 jQuery Form 进行变形

python - Python中3d中的点到凸包的距离

python - 在 python 中更改变量的类型是好习惯吗?

python - 替换txt文件中的整行

python - 来自 Keras 应用程序模块的 TensorFlow/Keras : How to get missing models (ResNet101, ResNeXt 等)?

python - Beautiful Soup - 获取所有文本,但保留链接 html?