Neil G
Neil G

Reputation: 33202

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.

Upvotes: 3

Views: 7189

Answers (5)

PaulMcG
PaulMcG

Reputation: 63709

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]

Upvotes: 1

dting
dting

Reputation: 39287

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]]

Upvotes: 0

A. Jesse Jiryu Davis
A. Jesse Jiryu Davis

Reputation: 24009

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

Upvotes: 5

Utku Zihnioglu
Utku Zihnioglu

Reputation: 4873

>>> 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.

Upvotes: 1

Tom
Tom

Reputation: 2016

type pydoc -p 5000 into the command line then got to http://localhost:5000/string.html#Formatter-parse

Upvotes: 0

Related Questions