简体   繁体   中英

String parsing with Python

I can't seem to find any docs on the parse() method for strings. Is there a good reference? I want to parse the following:

frame 0 rows {3 2 3 3 3 3 2 3 2 3 3 3 2 3 2 3 4 3 3 4 3 2 2 3 3 3 2 2 2 2 3 3 3 2 3 2 3 3 3 3 4 3 4 3 3 3 3 4 3 2 3 3 3 3 2 2 2 4 4 3 3 3 3 3 4 4 4 3 2 4 3 4 3 3 3 4 3 3 4 3 3 4 4 3 3 3 4 4 3 4 3 3 3 3 3 4} columns {2 3 2 3 3 3 4 3 3 2 3 2 2 2 3 2 3 3 2 2 2 3 3 3 3 2 3 3 3 2 3 3 2 2 2 3 3 4 3 3 3 3 3 3 3 3 2 3 3 3 3 4 3 2 3 2 3 3 3 3 3 2 2 3 3 3 3 2 3 3 3 3 3 3 3 3 3 4 3 3 3 3 3 4 3 3 4 3 4 4 4 3 4 4 4 4 4 4 3 3 4 4 3 4 4 4 4 3 3 3 4 4 3 4 4 3 3 4 3 5 5 5 5 4 5 4 4 4}

into two lists of int.

Python strings' parse() won't help you here (it has a very obscure use). In this case, I'd do it the obvious way: With regexes! If 's' is your string above,

import re
lists = [
    [int(i) for i in match.split()]
    for match in re.findall(r'{(.*?)}', s)
]

print lists
>>> a="frame 0 rows {3 2 3 3 3 3 2 3 2 3 3 3 2 3 2 3 4 3 3 4 3 2 2 3 3 3 2 2 2 2 3 3 3 2 3 2 3 3 3 3 4 3 4 3 3 3 3 4 3 2 3 3 3 3 2 2 2 4 4 3 3 3 3 3 4 4 4 3 2 4 3 4 3 3 3 4 3 3 4 3 3 4 4 3 3 3 4 4 3 4 3 3 3 3 3 4} columns {2 3 2 3 3 3 4 3 3 2 3 2 2 2 3 2 3 3 2 2 2 3 3 3 3 2 3 3 3 2 3 3 2 2 2 3 3 4 3 3 3 3 3 3 3 3 2 3 3 3 3 4 3 2 3 2 3 3 3 3 3 2 2 3 3 3 3 2 3 3 3 3 3 3 3 3 3 4 3 3 3 3 3 4 3 3 4 3 4 4 4 3 4 4 4 4 4 4 3 3 4 4 3 4 4 4 4 3 3 3 4 4 3 4 4 3 3 4 3 5 5 5 5 4 5 4 4 4}"

>>> import ast
>>> import re
>>> for match in re.finditer("\{([\d ]+)\}",a):
    integers=match.groups()[0]
    l=ast.literal_eval(integers.replace(" ",","))
    print l


(3, 2, 3, 3, 3, 3, 2, 3, 2, 3, 3, 3, 2, 3, 2, 3, 4, 3, 3, 4, 3, 2, 2, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 2, 3, 2, 3, 3, 3, 3, 4, 3, 4, 3, 3, 3, 3, 4, 3, 2, 3, 3, 3, 3, 2, 2, 2, 4, 4, 3, 3, 3, 3, 3, 4, 4, 4, 3, 2, 4, 3, 4, 3, 3, 3, 4, 3, 3, 4, 3, 3, 4, 4, 3, 3, 3, 4, 4, 3, 4, 3, 3, 3, 3, 3, 4)
(2, 3, 2, 3, 3, 3, 4, 3, 3, 2, 3, 2, 2, 2, 3, 2, 3, 3, 2, 2, 2, 3, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 2, 2, 2, 3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 4, 3, 2, 3, 2, 3, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 3, 3, 3, 4, 3, 3, 4, 3, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 3, 3, 4, 4, 3, 4, 4, 4, 4, 3, 3, 3, 4, 4, 3, 4, 4, 3, 3, 4, 3, 5, 5, 5, 5, 4, 5, 4, 4, 4)

I have never heard of a parse method to actually parses the string in the way you ask. However, parsing that string is not that hard. Here is how to do it.

For such nicely structured data, pyparsing may be more than you need, but it makes for a good tutorial example:

from pyparsing import *

s = "frame 0 rows {3 2 3 3 3 3 2 3 2 3 3 3 2 3 2 3 4 3 3 4 3 2 2 3 3 3 2 2 2 2 3 3 3 2 3 2 3 3 3 3 4 3 4 3 3 3 3 4 3 2 3 3 3 3 2 2 2 4 4 3 3 3 3 3 4 4 4 3 2 4 3 4 3 3 3 4 3 3 4 3 3 4 4 3 3 3 4 4 3 4 3 3 3 3 3 4} columns {2 3 2 3 3 3 4 3 3 2 3 2 2 2 3 2 3 3 2 2 2 3 3 3 3 2 3 3 3 2 3 3 2 2 2 3 3 4 3 3 3 3 3 3 3 3 2 3 3 3 3 4 3 2 3 2 3 3 3 3 3 2 2 3 3 3 3 2 3 3 3 3 3 3 3 3 3 4 3 3 3 3 3 4 3 3 4 3 4 4 4 3 4 4 4 4 4 4 3 3 4 4 3 4 4 4 4 3 3 3 4 4 3 4 4 3 3 4 3 5 5 5 5 4 5 4 4 4}"

LBRACE,RBRACE = map(Suppress,"{}")
integer = Word(nums).setParseAction(lambda t:int(t[0]))

line = ("frame" + integer("frame") + 
        "rows" + LBRACE + ZeroOrMore(integer)("rows") + RBRACE + 
        "columns" + LBRACE + ZeroOrMore(integer)("columns") + RBRACE )

data = line.parseString(s)
print data.frame
print data.rows[:10]
print data.columns[:10]

prints:

0
[3, 2, 3, 3, 3, 3, 2, 3, 2, 3]
[2, 3, 2, 3, 3, 3, 4, 3, 3, 2]

在命令行输入pydoc -p 5000然后到http:// localhost:5000 / string.html#Formatter-parse

m_string = "frame 0 rows {3 2 3 3 3 3 2 3 2 3 3 3 2 3 2 3 4 3 3 4 3 2 2 3 3 3 2 2 2 2 3 3 3 2 3 2 3 3 3 3 4 3 4 3 3 3 3 4 3 2 3 3 3 3 2 2 2 4 4 3 3 3 3 3 4 4 4 3 2 4 3 4 3 3 3 4 3 3 4 3 3 4 4 3 3 3 4 4 3 4 3 3 3 3 3 4} columns {2 3 2 3 3 3 4 3 3 2 3 2 2 2 3 2 3 3 2 2 2 3 3 3 3 2 3 3 3 2 3 3 2 2 2 3 3 4 3 3 3 3 3 3 3 3 2 3 3 3 3 4 3 2 3 2 3 3 3 3 3 2 2 3 3 3 3 2 3 3 3 3 3 3 3 3 3 4 3 3 3 3 3 4 3 3 4 3 4 4 4 3 4 4 4 4 4 4 3 3 4 4 3 4 4 4 4 3 3 3 4 4 3 4 4 3 3 4 3 5 5 5 5 4 5 4 4 4}"

import re

print [[ int(i) for i in  x.split(" ")] for x in [ match for match in re.findall("\{([\d ]+)\}", m_string)] ]

results in:

[[3, 2, 3, 3, 3, 3, 2, 3, 2, 3, 3, 3, 2, 3, 2, 3, 4, 3, 3, 4, 3, 2, 2, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 2, 3, 2, 3, 3, 3, 3, 4, 3, 4, 3, 3, 3, 3, 4, 3, 2, 3, 3, 3, 3, 2, 2, 2, 4, 4, 3, 3, 3, 3, 3, 4, 4, 4, 3, 2, 4, 3, 4, 3, 3, 3, 4, 3, 3, 4, 3, 3, 4, 4, 3, 3, 3, 4, 4, 3, 4, 3, 3, 3, 3, 3, 4], [2, 3, 2, 3, 3, 3, 4, 3, 3, 2, 3, 2, 2, 2, 3, 2, 3, 3, 2, 2, 2, 3, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 2, 2, 2, 3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 4, 3, 2, 3, 2, 3, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 3, 3, 3, 4, 3, 3, 4, 3, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 3, 3, 4, 4, 3, 4, 4, 4, 4, 3, 3, 3, 4, 4, 3, 4, 4, 3, 3, 4, 3, 5, 5, 5, 5, 4, 5, 4, 4, 4]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM