Python 2.7 打印字符串

标签 python

我想用下面的例子打印所有的名字。名称每次都是随机的。

txt = "[something name=\"Paul\" other=\"1/1/1\"][something name=\"James\" other=\"4/3/5\"][something name=\"Victor\" other=\"7/2/6\"][something name=\"Jane\" other=\"4/3/6\"]"

我知道如何打印第一个名字:

print str(txt[txt.index('[something name=\"')+17:txt.index(' other')-1])

但是我怎样才能全部打印出来呢?我需要在新行中打印所有名称:

Paul
James
Victor
Jane

最佳答案

看起来你可以在这里使用正则表达式:

import re
txt = "[something name=\"Paul\" other=\"1/1/1\"][something name=\"James\" other=\"4/3/5\"][something name=\"Victor\" other=\"7/2/6\"][something name=\"Jane\" other=\"4/3/6\"]"
for name in re.findall('name\=\\"(.*?)\\\"', txt):
    print name

打印:

Paul
James
Victor
Jane

关于Python 2.7 打印字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32761438/

相关文章:

python - 为什么使用 pandas.read_json 读取字符串整数不正确?

python - pandas 使用 python3 读取 csv 标准化日期

Python pymysql - 遍历 mysql 表键和值

python - 当使用 cron 管理程序时,从 cron 作业运行脚本的行为与使用 django manage.py 从命令行运行时的行为不同

python - 正则表达式:如果后面跟着一组运算符,如何捕获括号组?

python - 使 Python 类在创建新属性时抛出错误

python - 在python中使用默认消息引发内置异常

python - 将 str 列表排序为 str 对,其中一个 str 具有 -R

python - 额外线程的装饰器

python - pandas dataframe .loc 行值