I’m designing a weather program where I need to keep track of certain things and allow the user to add data which will be saved and subsequently read later. My fields are
City
State
Zip
Metar
I might have more I want to do with this configuration file later, so I would like it to have something like this:
[LOCATIONS]
Phoenix:AZ:85001:KPHX
Dallas:TX:75201:KDFW
[USER CONFIGS]
for later
Setting up to write the file shouldn’t be very hard, just create the file object to write to, write the [Locations] followed by the data that I plan on keeping together in lists and just join the lists with the colon.
However, I’m a little stumped on how to read it back in.
I know I can read the line in one at a time and check if line == ‘[LOCATIONS]’ for example, and read the lines after until I come to a blank line, but what if there is an extra blank line. Or should I do a startswith(‘[‘) and just not add blank lines.
Tried to do some googling online because this looks like it would be a fairly frequent problem for new programers, but I have come up empty (except some learning about the CVS module, which I don’t think can help me here).
A formal database using sqlite would be overkill. At most there will likely be 20-50 entries for each user, most would be less.
Also in the program, each item of data (except state) entered in the GUI can result in all the others being updated with the correct values. Since almost every data item can act as a lead, I’m not sure how I should set this up for easy searching. Should I make a dictionary for City that would include a structure like {‘Dallas’:(‘TX’, ‘75201’, ‘KDFW’)} to help with lookup one for Metars {‘KDFW’: (‘Dallas’, ‘TX’, ‘75201’)} and Zips {‘75201’: (‘DALLAS’, ‘TX’, ‘KDFW’)}? This seems a little better than iterating over each line to check for a match, but seems a bit of a waste considering the original structure will be a list like [‘Dallas’, ‘TX’, ‘75201’, ‘KDFW’]. What would be nice as a dictionary-like object that could have each item in it act like a key returning the other values in the association.
Various thoughts and ideas are appreciated.
To store data, you may use XML. Then read it off using any XML parser, SAX or DOM which are included with python.
Since the size of data is very less (only around 20-25 entries per user), you can take the approach of first knowing about the search term, whether its state name or whether its pin code etc. (Ask user to enter this in GUI).
Assuming that data is stored as [City State Pin Mater], you can then search in the respective column. e.g if the user enters 12345, and you know it’s a pin, you only need to search in the 3rd index of the list-data and then return the list. For state name you would search in the 2nd column.
And this approach works even if the number of records is large, say a couple of hundreds.