On Thu, Sep 8, 2011 at 10:03 AM, Alan Gauld <alan.ga...@btinternet.com>wrote:
> On 08/09/11 11:58, Richard D. Moores wrote: > >> I've succeeded in writing a dictionary ({}) that I can use as a small >> personal phone book. The dictionary (very shortened and simplified) >> looks like this in the script; >> >> p = {} >> >> p['bp1'] = 'xxx' >> p['bp2'] = 'ooo' >> p['ch'] = 'zzz' >> p['me'] = 'aaa' >> p['mg'] = 'vvv' >> p['pu1'] = 'bbb' >> p['pu2'] = 'ccc' >> p['pw'] = 'kkk' >> >> > You could have done that in one line if you preferred: > > > p = { > 'bp1':'xxx', > 'bp2':'ooo' > etc/... > 'pw':'kkk' > > } > > > But I'd like to put the lines of the dictionary in a text file so that >> I can add key/value items to it by writing to it with another script. >> > > Consider using a shelve (See the shelve module) > It is basically a file that you can treat as a dictionary... > > Try: > > >>> import shelve > >>> help(shelve) > > For examples and info. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > ______________________________**_________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor> > Another option would be for you to use XML and base it off of a schema. There's a really nifty tool called generate DS (just google it) that will turn any valid schema into a python module. I pasted an example schema that you might use here: http://pastebin.com/AVgVGpgu Once you install generateds, just cd to where it is installed and type: python generateds.py -o pn.py PhoneNumber.xsd This assumes you named the above schema as PhoneNumber.xsd That will create a file called pn.py. For some reason it generated a bad line on line 452, which I just commented out. I then made a script in just a few lines to make a phonebook: http://pastebin.com/h4JB0MkZ This outputs XML that looks like this: <PhoneBook> > <Listing> > <PersonsName>aaa</PersonsName> > <Number Type="Mobile"> > <Number>1231231234</Number> > </Number> > </Listing> > <Listing> > <PersonsName>bbb</PersonsName> > <Number Type="Work"> > <Number>1231231234</Number> > </Number> > <Number Type="Home"> > <Number>6789231234</Number> > </Number> > </Listing> > <Listing> > <PersonsName>ccc</PersonsName> > <Number Type="Fax"> > <Number>1231231234</Number> > </Number> > </Listing> > <Listing> > <PersonsName>ddd</PersonsName> > <Number Type="Home"> > <Number>1231231234</Number> > </Number> > </Listing> > </PhoneBook> The advantage is, you can immediately grab the generated xml and create python instances using the build() method for further editing. You would of course need to create a schema that fits your needs. Mine was just a quick and dirty example of what you could do.
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor