Reputation: 1161
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) ?
Upvotes: 1
Views: 87
Reputation: 19153
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)
Upvotes: 1
Reputation: 82795
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
Upvotes: 0