[Tutor] output of dictionaries
I'm creating a small database using a dictionary of dictionaries, and I want to output it to a file. It seems that only strings can be output to files, and I cannot quite figure out how to quickly and simply convert my dictionary to a list of strings or whatever. Or maybe I'm going about this all wrong. So, the most general form of my question: how can I store a dictionary containing some data records stored as dictionaries - a dictionary of dictionaries - in a file, so I can read it back in later and process its contents? Thank you very much for your generosity in giving some of your time to this service. I'm very grateful. -- t. ====== Tom Cloyd Bellingham, Washington, U.S.A: (360) 920-1226 << BestMindHealth.com >> == Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pickling in plain English
I just want to take a moment to express my deep appreciation for this List. As one who is learning Python as amateur, in my spare moments, and already getting it to do good work for me, these clear, beautifully worked descriptions of basic aspects of Python are simply an ongoing delight for me - and singularly useful to my study of Python. To those of you who give your time, thought, and experience here to those of us who need it, thank you so very much. Tom Cloyd On Tue, 24 May 2005 16:32:50 -0700, <[EMAIL PROTECTED]> wrote: > Quoting Tom Tucker <[EMAIL PROTECTED]>: > >> I am having trouble understanding the c|Pickle modules. What does >> serializing and de-serializing objects mean? I have read the below >> urls and I "think" I understand the process, but I can't visualize the >> beneifts. Can someone kindly explain pickling in lamens terms? > > It's actually astonishingly simple, once you get the hang of it. > > example: > >>>> arr = [1, 3, 5, 7] >>>> dct = {'foo':1, 'bar':2, 'baz':3} >>>> st = "re: your mail" >>>> >>>> import pickle >>>> f = file('dump', 'wb') >>>> pickle.dump((arr, dct, st), f) >>>> f.close() >>>> ^Z > > [new instance of python] > >>>> import pickle >>>> f = file('dump', 'r') >>>> res = pickle.load(f) >>>> res > ([1, 3, 5, 7], {'bar': 2, 'foo': 1, 'baz': 3}, 're: your mail') > > [and you can look at the file 'dump' on your HDD, if you want to] > > Basically, pickle allows you to write any python object to disk and the > load it > back again with very little effort. This is important if you want your > program > to be able to save state between instances. > > You can pickle more complex objects; the only restriction is that > pickle.load > must be able to find the module where the classes are defined. > > [there are some other restrictions, but you can do a lot without > worrying about > them] > > Does this help? > -- == Tom Cloyd Bellingham, Washington, U.S.A: (360) 920-1226 << BestMindHealth.com >> == Using Opera's revolutionary e-mail client (program): http://www.opera.com/mail/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] oops!
Sorry about that last post! I thought I was replying to a Komodo list post. -- t. == Tom Cloyd Bellingham, Washington, U.S.A: (360) 920-1226 << BestMindHealth.com >> == Using Opera's revolutionary e-mail client (program): http://www.opera.com/mail/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] mscvrt module question
Executing within Komodo 3.1, running python 2.4.1, it appears that when running my source file, python knows kbhit(), but not getch(). How can this be? KeyIn = getch() File "C:\Program Files\ActiveState Komodo 3.1\dbgp\pythonlib\dbgp\client.py", line 1799, in runMain self.dbg.runfile(debug_args[0], debug_args) File "C:\Program Files\ActiveState Komodo 3.1\dbgp\pythonlib\dbgp\client.py", line 1524, in runfile h_execfile(file, args, module=main, tracer=self) File "C:\Program Files\ActiveState Komodo 3.1\dbgp\pythonlib\dbgp\client.py", line 590, in __init__ execfile(file, globals, locals) File "C:\Documents and Settings\Tom C\My Documents\Python projects - Toms\quiz\quiz.py", line 13, in __main__ KeyIn = getch() NameError: name 'getch' is not defined Also, the interpreter know neither. I don't get it -- a=kbhit() Traceback (most recent call last): [snip] File "", line 0, in __main__ NameError: name 'defined' is not defined I don't know how to fix this, so any suggestions would be much appreciated. -- t. == Tom Cloyd Bellingham, Washington, U.S.A: (360) 920-1226 << BestMindHealth.com >> == Using Opera's revolutionary e-mail client (program): http://www.opera.com/mail/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] mysterious Python error
I'm working on a WinXP SP-2 OS. I'm too ill-informed to figure this out: When I execute C:\Python24\python.exe I get 'import site' failed.; use -v for traceback ActivePython 2.4.1 Build 247 ... I then ran this from a DOS window, and tried to redirect the -v output to a text file, so I could include it, but nothing I did was successful. I can't imagine why the output wouldn't redirect, but it would not. the file remained empty. The output from the python.exe -v command was verbose. The import that failed was "import sitecustomize", which is in ..\lib\sitepackages\sitecustomize.py There was also protest about another line in that file: sys setdefaultencoding('utf-8') -- "NameError: name 'sys' is not defined." The interpreter DID become active, after all this. I attempted to fix this by removing and reinstalling the ActivePython 2.4.1 Windows package. It didn't change anything at all. Can anyone suggest what I might do with this? Thanks for your help. -- Tom == Tom Cloyd, MS MA, LMHC Private practice Psychotherapist Bellingham, Washington, U.S.A: (360) 920-1226 << BestMindHealth.com / [EMAIL PROTECTED] >> == Using Opera's revolutionary e-mail client (program): http://www.opera.com/mail/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] use of webbrowser.open_new()
Having just discovered the webbrowser module, I've confirmed that I can indeed open a URL in my default browser using something like webbrowser.get_new("{URL}") What I want to do is open a web page stored on my local machine. I'm not getting it to work - webbrowser.open_new("C:\__Library\folders\05238 Design for Confusion\05238 Design for Confusion -05krugman.html") Traceback (most recent call last): File "C:\Program Files\ActiveState Komodo 3.1\dbgp\pythonlib\dbgp\client.py", line 3149, in runcode locals = self.frame.f_locals) File "C:\Program Files\ActiveState Komodo 3.1\dbgp\pythonlib\dbgp\client.py", line 1569, in runcode h_exec(code, globals=globals, locals=locals, module=module) File "C:\Program Files\ActiveState Komodo 3.1\dbgp\pythonlib\dbgp\client.py", line 516, in __init__ exec code in globals, locals File "", line 0, in __main__ File "C:\Python24\Lib\webbrowser.py", line 46, in open_new get().open(url, 1) File "C:\Python24\Lib\webbrowser.py", line 250, in open os.startfile(url) WindowsError: [Errno 2] The system cannot find the file specified: 'C:\\__Library\x0colders*38 Design for Confusion*38 Design for Confusion -05krugman.html' Can you suggest what I might be doing wrong here? -- t. == Tom Cloyd Bellingham, Washington, U.S.A: (360) 920-1226 << BestMindHealth.com / [EMAIL PROTECTED] >> == Using Opera's revolutionary e-mail client (program): http://www.opera.com/mail/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] raw string - solution to open_new() problem
Ewald Ertl's reply to Don Parris's question about "r" operator gave me the idea of trying that out to solve my problem with file name scrambling when trying to use webbrowser.open_new() to open a file on my computer in a browser. It worked! So, thie webbrowser.open_new("file://C:\__Library\folders\02394 Yale Style Manual\02394 Yale_Style_Manual.htm") does not work, but this webbrowser.open_new(r"file://C:\__Library\folders\02394 Yale Style Manual\02394 Yale_Style_Manual.htm") does. Thank you Ewald, for triggering the solution in my mind! Now, if anyone can explain why webbrowser.open_new() does the character substitution thing it was doing (and thus becoming unable to locate the file in question), I'm eager to hear it. Meanwhile, I can go ahead with my program. -- t. ========== Tom Cloyd, MS MA, LMHC Private practice Psychotherapist Bellingham, Washington, U.S.A: (360) 920-1226 << BestMindHealth.com / [EMAIL PROTECTED] >> == Using Opera's revolutionary e-mail client (program): http://www.opera.com/mail/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] surprising len() results ???
List, I'm running a program I wrote which I've run a number of times before, successfully. I use the following statement to fix the problem of utf-8 codes which blow up a later part of my production process that I cannot fix: trans_table = maketrans('()ÄäÀÁàáÇçÈÉèéÌÍìíÑñÒÓòóÙÚúù','--AaAAaaCcEEeeIIiiNnOOooUUuu') As I said, this has been working fine. But no longer. I previously have been running the ActivePython-2.4.1-247-win32 Python from ActiveState, and executing my program in ActiveState's Komodo IDE. Due to some problems with the Boa Constructor IDE, I removed ActivePython and ran the Python 2.4.1 Windows installer from python.org. I mention this because I'm wondering if this might be behind my problem. I'm still running from within Komodo. Today, when running my program, when execution gets to the maketrans() function above, I get: Traceback (most recent call last): File "C:\Program Files\ActiveState Komodo 3.1\dbgp\pythonlib\dbgp\client.py", line 1799, in runMain self.dbg.runfile(debug_args[0], debug_args) File "C:\Program Files\ActiveState Komodo 3.1\dbgp\pythonlib\dbgp\client.py", line 1524, in runfile h_execfile(file, args, module=main, tracer=self) File "C:\Program Files\ActiveState Komodo 3.1\dbgp\pythonlib\dbgp\client.py", line 590, in __init__ execfile(file, globals, locals) File "C:\Documents and Settings\Tom C\My Documents\Python projects - Toms\li-database\LI-db-use--dict_db.py", line 49, in __main__ trans_table = maketrans('()ÄäÀÁàáÇçÈÉèéÌÍìíÑñÒÓòóÙÚúù','--AaAAaaCcEEeeIIiiNnOOooUUuu') ValueError: maketrans arguments must have same length This makes no sense to me. So, I then broke up the maketrans() function statement's two parameters, and ran these two statements from within the program: print len('()ÄäÀÁàáÇçÈÉèéÌÍìíÑñÒÓòóÙÚúù') print len('--AaAAaaCcEEeeIIiiNnOOooUUuu') the result: 54 28 I'm completely mystified by this. All of it. None of it makes sense. This program was working fine. Now it doesn't. And those two parameter string are plainly the same length, but Python doesn't think so. All help will be gratefully accepted! -- t. == Tom Cloyd Bellingham, Washington, U.S.A: (360) 920-1226 << BestMindHealth.com / [EMAIL PROTECTED] >> == Using Opera's revolutionary e-mail client (program): http://www.opera.com/mail/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] surprising len() results ???
Michael and List, You suggestion (see foot of email trail, below) is a bit over my head. As I stated, I removed ActivePython 2.4.1 installed Python 2.4.1 from python.org, and now my program doesn't work, where before it did. I don't understand this. When isn't Python 2.4.1 == Python 2.4.1? Is this a Python level problem, or something in my script? At the head of my script I explicitly set coding to utf-8, but that't not new. Do I need to change a parameter somewhere? More to the point, what do I have to do to get going again. I really don't know. Thanks in advance for any help! Tom C. On Tue, 09 Aug 2005 03:33:11 -0700, Michael Janssen <[EMAIL PROTECTED]> wrote: > On 8/9/05, Tom Cloyd <[EMAIL PROTECTED]> wrote: > >> print len('()ÄäÀÁàáÇçÈÉèéÌÍìíÑñÒÓòóÙÚúù') >> print len('--AaAAaaCcEEeeIIiiNnOOooUUuu') >> >> the result: >> >> 54 >> 28 >> >> I'm completely mystified by this. All of it. None of it makes sense. >> This >> program was working fine. Now it doesn't. And those two parameter string >> are plainly the same length, but Python doesn't think so. > > on my computer (python 2.3): >>>> s = 'ä' # a-umlaut >>>> len(s) > 1 >>>> len(s.decode('latin-1')) # prepare for utf-8 encoding > 1 >>>> len(s.decode('latin-1').encode('utf-8')) > 2 >>>> len('a'.decode('latin-1').encode('utf-8')) > 1 > > seems like len returns the number of bytes and some encodings uses > more than one byte for certain chars. You can proberbly decode your > strings from utf-8 (or whatever encoding you use (and perhaps encode > it back into a one-char-one-byte encoding [on my system the decoded > (unicode) string is just fine]). > > regards > Michael > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- == Tom Cloyd, MS MA, LMHC Private practice Psychotherapist Bellingham, Washington, U.S.A: (360) 920-1226 << BestMindHealth.com / [EMAIL PROTECTED] >> == Using Opera's revolutionary e-mail client (program): http://www.opera.com/mail/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor