Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using python to parse colon (:) delimited string to an object

Tags:

python

i have a string which is a return value of REST API (http://requesttracker.wikia.com/wiki/REST) and is using colon seperated key/value pairs.

id: 123414
name: Peter
message: bla bla
  bla bla

How can i parse this string to an object? is there an existing python parser that i can use??

Here's the string that i want to parse:

'RT/3.8.8 200 Ok\n\nid: ticket/46863\nQueue: customer-test\nOwner: Nobody\nCreator: young.park\nSubject: testing\nStatus: new\nPriority: 0\nInitialPriority: 0\nFinalPriority: 0\nRequestors: [email protected]\nCc:\nAdminCc:\nCreated: Mon Apr 25 15:50:27 2011\nStarts: Not set\nStarted: Not set\nDue: Not set\nResolved: Not set\nTold: Not set\nLastUpdated: Mon Apr 25 15:50:28 2011\nTimeEstimated: 0\nTimeWorked: 0\nTimeLeft: 0\nCF.{Severity}: \n\n'
like image 514
CIF Avatar asked Feb 03 '26 13:02

CIF


2 Answers

You really need to say which REST api and provide a documentation reference.

Superficially, it doesn't look too hard:

# Look Ma, no imports!
>>> s = 'id: 1234\nname: Peter\nmessage: foo bar zot\nmsg2: tee:hee\n'
>>> dict(map(str.strip, line.split(':', 1)) for line in s.splitlines())
{'message': 'foo bar zot', 'msg2': 'tee:hee', 'id': '1234', 'name': 'Peter'}

But: (1) the documentation should point you at a parser (2) nothing is ever as easy as it seems from one simple example (see tee:hee above); if you decide on rolling your own, you should break the above one-liner up into multiple steps so that you can do some error checking (e.g. line.split() returns exactly 2 pieces).

Update after api reference was given:

At first glance, the website gives an enormous number of examples without actually stating what the format is. I suggest that you give it more than a glance; if that fails, ask the author/maintainer.

Update 2 after actual example input given, and after comment "I just tried this and got crashed":

The code supplied was in response to the first (ambiguous) example input, in which all lines except the last contained a colon. It was accompanied by a suggestion that it should be done in pieces instead of a one-liner with especial mention of checking the result of split(':', 1). What code did you use? What exactly does "got crashed" mean? Have you tried to work out for yourself what your problem was, and fix it?

What data did you feed it? Your long-awaited actual sample has colon-separated key:value lines preceded by a heading line and an empty line and followed by an empty line. These can be blissfully ignored by a trivial adjustment to the one-liner:

>>> print dict(map(str.strip, line.split(':', 1)) for line in s.splitlines()[2:-1])
{'Status': 'new', 'Resolved': 'Not set', 'CF.{Severity}': '',
'TimeLeft': '0', 'Creator': 'young.park', 'Cc': '', 'Starts': 'Not set',
'Created': 'Mon Apr 25 15:50:27 2011', 'Due': 'Not set',
'LastUpdated': 'Mon Apr 25 15:50:28 2011', 'Started': 'Not set',
'Priority': '0', 'Requestors': '[email protected]',
'AdminCc': '', 'Owner': 'Nobody', 'Told': 'Not set',
'TimeEstimated': '0', 'InitialPriority': '0', 'FinalPriority': '0',
'TimeWorked': '0', 'Subject': 'testing'}
>>>

Note 1: above output edited manually to avoid horizontal scrolling.

Note 2: Includes the Created and LastUpdated entries (-:whose values contain colons:-)

If you don't believe in blissfully ignoring things, you can do the splitlines first, and assert that the first line contains something like the expected heading, and that the second and last lines are empty.

like image 178
John Machin Avatar answered Feb 06 '26 04:02

John Machin


That looks like YAML. Have you tried PyYAML?

>>> import yaml
>>> s = """id: 123414
... name: Peter
... message: bla bla
...   bla bla"""
>>> yaml.load(s)
{'message': 'bla bla bla bla', 'id': 123414, 'name': 'Peter'}
like image 32
jathanism Avatar answered Feb 06 '26 04:02

jathanism