I just started using/learning Python and have some questions.
I have a text file generated by the reporting tool.
The file contains some stuff like this. There are many stuff which basically follow the same format as the one written below.
Format=
{
Window_Type=”Tabular”,
Tabular=
{ Num_row_labels=5
}
}
There are named value attributes in this file.
For e.g., Window_Type is the name of the attribute having value Tabular.
Again for Tabular named attribute has a value 5 associated with it.
What I want to be able to do is Open up the file.
- Check if Window_Type is Tabular
- If yes, then check the Num_row_labels associated with Tabular.
- If Num_row_lables has a value greater than or equal to 5, then print the name of the text file and the path of the folder, where that file exists.
I m using Python 3.2 in Eclipse 3.7.2 IDE.
For a testing part, I imported my text file in the IDE and used the code below to read the file. In future I should be able to traverse the folder/s where the files with extension mrk are located.(This will be a known directory because we keep those files in there.) Please kindly help me out. Thanks a bunch!
import os.path
fn = os.path.join(os.path.dirname(__file__), 'Multitab.mrk')
with open(fn, 'r') as file:
print(file.read())
Please note that this answer is a very dirty hack. That format is almost similar to JSON format, which prompted me to write an RE which will convert the string such that it can be cleanly parsed by json parser. This is what I did:
OUTPUT: