0

I have text line by line which contains many field name and their value seperated by : , if any line does not have any field value then that field would not exist in that line for example

First line:
A:30 B: 40 TS:1/1/1990 22:22:22
Second line
A:30 TS:1/1/1990 22:22:22
third line
A:30 B: 40

But it is confirmed that at max 3 fields are possible in single line and their name will be A,B,TS. while writing python script for this, i am facing below issues: 1) I have to extract from each line which are the field exist and what are their values 2) Field value of field TS also have seperator ' '(SPACE).so unable retrieve full value of TS(1/1/1990 22:22:22)

Output valueshould be extracted like that

First LIne:
A=30
 B=40
 TS=1/1/1990 22:22:22

Second Line:
A=30

 TS=1/1/1990 22:22:22

Third Line
A=30
 B=40

Please help me in solving this issue.

1
  • 1
    That's true, but is this a reason to downvote his question? I find it perfectly valid. Also, if you keep downvoting him, he loses his right to upvote, and we wouldn't want that, would we? :) Commented Nov 4, 2010 at 9:04

2 Answers 2

2
import re
a = ["A:30 B: 40 TS:1/1/1990 22:22:22", "A:30 TS:1/1/1990 22:22:22", "A:30 B: 40"]
regex = re.compile(r"^\s*(?:(A)\s*:\s*(\d+))?\s*(?:(B)\s*:\s*(\d+))?\s*(?:(TS)\s*:\s*(.*))?$")
for item in a:
    matches = regex.search(item).groups()
    print {k:v for k,v in zip(matches[::2], matches[1::2]) if k}

will output

{'A': '30', 'B': '40', 'TS': '1/1/1990 22:22:22'}
{'A': '30', 'TS': '1/1/1990 22:22:22'}
{'A': '30', 'B': '40'}

Explanation of the regex:

^\s*      # match start of string, optional whitespace
(?:       # match the following (optionally, see below)
 (A)      # identifier A --> backreference 1
 \s*:\s*  # optional whitespace, :, optional whitespace
 (\d+)    # any number --> backreference 2
)?        # end of optional group
\s*       # optional whitespace
(?:(B)\s*:\s*(\d+))?\s*  # same with identifier B and number --> backrefs 3 and 4
(?:(TS)\s*:\s*(.*))?     # same with id. TS and anything that follows --> 5 and 6
$         # end of string
Sign up to request clarification or add additional context in comments.

3 Comments

in the continuation of above thread ,i have below line | | | A:720897 | N°227: AT CIRCLE and i have used regex = re.compile(r"\s*(?:(Link Id)\s*:\s*(\d+))\s*|\s*(?:(N°(\d+))\s*:\s*(.*))$") but it is not giving desired result .pls let me know where i am wrong.i have used # -- coding: iso-8859-1 --
@james: Code formatting is difficult in comments; could you edit your answer and format your new examples as code, so I can see better where the problem might be? Thanks.
i have put this question in seperate thread please refer question "Regex to find out fields which are having non ascii character in python" to see my issue
1

You could use regular expressions, something like this would work if the order was assumed the same every time, otherwise you would have to match each part individually if you're unsure of the order.

import re

def parseInput(input):
    m = re.match(r"A:\s*(\d+)\s*B:\s*(\d+)\s*TS:(.+)", input)
    return {"A": m.group(1), "B": m.group(2), "TS": m.group(3)}

print parseInput("A:30 B: 40 TS:1/1/1990 22:22:22")

This prints out {'A': '30', 'B': '40', 'TS': '1/1/1990 22:22:22'} Which is just a dictionary containing the values.

P.S. You should accept some answers and familiarize yourself with the etiquette of site and people will be more willing to help you out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.