Answer the question
In order to leave comments, you need to log in
How to compose a python regular expression?
there is a line like this:
''first:
numbers separated by commas
second:
numbers separated by commas"
I just can't figure out how to write a regular expression to collect separately all the numbers first and second in 2 different lists. first=re.findall('\d+', data)
collects all the numbers.
If so: first=re.findall('(\d+)\n\nsecond', data)
then puts to the first variable only the last number from first in the general data list.
Answer the question
In order to leave comments, you need to log in
First you need to find the necessary lines, and then parse them.
>>> import re
>>>
>>> text = """
... abc
... first:
... 10, 20, 30, 40, 50
...
... def
... second:
... 600, 700, 800, 900, 1000
...
... ghi
... """
>>>
>>> list(map(re.compile(r'\d+').findall,
... re.findall(r'(?:\d+(?:, )?)+', text)))
>>>
next_line_is_first, next_line_is_second = False, False
first, second = [], []
for line in file:
if next_line_is_first:
first = line.split(,)
next_line_is_first = False
if next_line_is_second:
second = line.split(,)
next_line_is_second = False
if 'first:' in line:
next_line_is_first = True
if 'second:' in line:
next_line_is_second = True
print first, second
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question