So the title sounds odd because perhaps my problem is odd... I have a .txt file with thousands of lines of machine output from a different program in the following format:
candidates(6,1,0,5,[ev(-1000,'C0009814','Stenosis','Acquired stenosis',[stenosis],[patf])])
Essentially we have 'candidates' marking the start of a tuple, and 'ev' marking the start of a second tuple inside of a single element list. When I read all of this into python from the file, it reads in as a string. But I need an object so I can access the nth index of the tuple. Truly, I would be happy just finding a way to consistently get the last value of the ev() tuple from this string, in this case 'patf'.
I had considered just splitting on ',' but this is not always successful because the list inside the list '[stenosis]', can sometimes have values like '[regurgitation, aortic]'. That extra ',' throws off the list index by 1 and therefore it return 'aortic]' instead of '[patf]'.
Please let me know if I can clarify anything or if I took some piece of knowledge for granted that needs to be said before this can be solved. Many thanks. I also included a second examples below that illustrates the problem of splitting on ','.
candidates(8,1,0,7,[ev(-875,'C0003501','Aortic Valve','Aortic valve structure',[aortic,valve],[bpoc])])
Edit: The object doesn't need to be a list, I guess. A tuple of the same format works well. Just as long as I can consistently reference one index for the info I need. Thanks!
Edit 2: I use python 2.7.6
If your data is always formatted the same way, the quickest way is to use regular expressions (module
re), if you know how to.Otherwise, and this is quite an unsightly hack, you can try to "parse" the data using
eval. Here's an example: