[Tutor] Problem with complex strings as key for a shelve-Dictionary

Kent Johnson kent37 at tds.net
Sun Sep 6 16:32:10 CEST 2009


On Sun, Sep 6, 2009 at 6:20 AM, markus kossner <m.kossner at tu-bs.de> wrote:
> Dear all,
> I have a Problem with complex strings as keys in a shelve Object:
>
> #!/usr/bin/python
> import shelve
> test=shelve.open('testshelve')
> test={
> 'CCN1c2ccccc2/C(=C2/S/C(=N\c3ccc(Cl)cc3)N(C)C2=O)C1=O':int(0),
> 'CN1/C(=N\c2ccc(Cl)cc2)S/C(=C2\C(=O)Nc3ccccc32)C1=O':int(20),
> 'c1cc2c(cc1)N(CCCl)C(=O)/C2=C1\S/C(=N/c2ccc(F)cc2)NC1=O':float(1),
> 'Cc1cccc(/N=C2/NC(=O)/C(=C3\c4c(cccc4)N(C)C3=O)S2)c1C':0.5,
> }

You just re-assigned test to be a plain dict, the result of
shelve.open() is lost. You want
test=shelve.open('testshelve')
test['CCN1c2ccccc2/C(=C2/S/C(=N\c3ccc(Cl)cc3)N(C)C2=O)C1=O'] = int(0)
etc.


> This works fine as long as I keep the shelve opened.
> However, when I then want to open the shelve with another python script
> I get key errors, as the different keys are not found:

Because you are not actually using the shelve, you are throwing away
immediately.

Kent


More information about the Tutor mailing list