I have a file which contains mostly of names mapping to a certain list of numbers. I would like to parse this file, and I presume a regular expression would be good here. I have however two problems:
-
The names can consist of either a word, or a word followed by more words, or a word with underscore. A word can also contain the following characters:
(/->) -
There is a comment in the beginning of the file and at some other places which should not be included in the result. The comments are always a line of
---followed by some text followed by another line of---
So if I have the following file
-----------------------------------
Comment
-----------------------------------
Ignore these lines
-----------------------------------
someVar 0.0 1.0
some var with spaces 52 93
another var_with_underscores 3
some var with (special->chars) 13 37 95
another char/slash 132
-----------------------------------
Another comment
-----------------------------------
yet another var 27.3 9
I want to return a dictionary
{"someVar": [0.0, 1.0],
"some var with spaces": [52, 93],
"another var_with_underscores": [3],
"another char/slash": [132]
"some var with (special->chars)": [13, 37, 95],
"yet another var": [27.3, 9]}
If it’s too much to ask for one question, I would be happy just knowing the regexp.
I’m using Python 2.7.
This may do what you’re looking for:
Which on your updated example give: