1

I have a database containing folders with files. I manage to read the files and get specific lines containing the montage word...

montage = 0, FP1-F7: EEG FP1-REF -- EEG F7-REF
montage = 1, F7-T3: EEG F7-REF -- EEG T3-REF
montage = 2, T3-T5: EEG T3-REF -- EEG T5-REF
montage = 3, T5-O1: EEG T5-REF -- EEG O1-REF

Now, I want to be able to extract what is between the comma and the double point (i.e. FP1-F7, F7-T3,...) but I don't know how to do ?

Moreover, why does the print command display lines with a "space" between (not shown below but real) ?

1
  • print appends a newline at the end by default, if you have a \n in your string, you'll have an empty line after the printed one. Commented Aug 6, 2018 at 10:08

2 Answers 2

1

Getting the token you want (note: this works with the samples you provided, if there's other commas or colons before the points you show, this won't work):

def parse_line(line):
  start = line.find(',')
  end = line.find(':')
  return line[start+1:end].strip()

You can also do this with regex, but IMO for this scenario that's overkilling.

Getting the list of tokens then can be done with a list comprehension:

tokens = [parse_line(l) for l in lines]

where lines is the list of lines to parse (or, if you're reading from a text file, the file object itself)

Sign up to request clarification or add additional context in comments.

1 Comment

I accept this one because it's more intuitive than using regex; thank you @GPhilo, it helps
0

Using Regex.

import re
s = """montage = 0, FP1-F7: EEG FP1-REF --  EEG F7-REF
montage = 1, F7-T3:  EEG F7-REF  --  EEG T3-REF
montage = 2, T3-T5:  EEG T3-REF  --  EEG T5-REF
montage = 3, T5-O1:  EEG T5-REF  --  EEG O1-REF """

for i in s.splitlines():
    m = re.search(r",(.*?):", i)     #Get content between , and :
    if m:
        print(m.group(1).strip())

Output:

FP1-F7
F7-T3
T3-T5
T5-O1

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.