Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I wonder if there's a way in python to be able to extract the informations from a line like the example below and put it in a table:

table = [ Time: 01:09:25.258, O:Localhost, R:192.168.1.1 id:62 ] 

data = "01:09:25.258 mta   Messages  I Doc O:Localhost   R:192.168.1.1   id:62 "
share|improve this question
2  
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be better answered, see the FAQ, especially How to Ask. – Inbar Rose Apr 17 at 8:56
well i want to split the data and put it in a table, i've tried this way but it doesn't work ( i'm beginner in python.. ) import re data = "01:09:25.258 mta Messages I Doc O:Localhost R:192.168.1.1 id:62 " id = re.finditer(r"\id:(\d+)\, data ) O = re.finditer(r"\O:(\d+)\, data ) R = re.finditer(r"\R:(\d+)\, data ) tab = [ id, O, R ] print tab – James H Apr 17 at 9:02
1  
I am sure you want to do a lot of things, but the garbage you posted above is useless. If you want help, you will need to post actual code, some actual examples, and not assume we can read your mind or know what you want to acomplish. – Inbar Rose Apr 17 at 9:03

closed as not a real question by Inbar Rose, Martijn Pieters, MattH, kaᵠ, Trott Apr 18 at 4:45

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

1 Answer

up vote 0 down vote accepted

For this task, I think using a dictionary would be a good idea:

import re

data = "Time:01.09.25.258 O:Localhost R:192.168.1.1 id:62 "

result = {}
for i in data.split():
    j, k = re.findall(r'[^: ]+', i)
    result[j] = k

print(result) #Prints {'id': '62', 'O': 'Localhost', 'Time': '01.09.25.258', 'R': '192.168.1.1'}
share|improve this answer
Great That was exactly what i was looking for now i'll have to edit it because my messages are much longer than that ;D thanks for your help ! – James H Apr 17 at 9:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.