... my first Python program! So please be gentle (no fifty ton weights on the head!), but tell me if it's properly "Pythonic", or if it's a dead parrot (and if the latter, how to revive it).
I'm working from Beazley's /Python: Essential Reference/ (2nd ed. 2001), so my first newbie question is how best to find out what's changed from version 2.1 to version 2.5. (I've recently installed 2.5.4 on my creaky old Win98SE system.) I expect to be buying the 4th edition when it comes out, which will be soon, but before then, is there a quick online way to find this out? Having only got up to page 84 - where we can actually start to read stuff from the hard disk - I'm emboldened to try to learn to do something useful, such as removing all those annoying hard tab characters from my many old text files (before I cottoned on to using soft tabs in my text editor). This sort of thing seems to work, in the interpreter (for an ASCII text file, named 'h071.txt', in the current directory): stop = 3 # Tab stops every 3 characters from types import StringType # Is this awkwardness necessary? detab = lambda s : StringType.expandtabs(s, stop) # Or use def f = open('h071.txt') # Do some stuff to f, perhaps, and then: f.seek(0) print ''.join(map(detab, f.xreadlines())) f.close() Obviously, to turn this into a generally useful program, I need to learn to write to a new file, and how to parcel up the Python code, and write a script to apply the "detab" function to all the files found by searching a Windows directory, and replace the old files with the new ones; but, for the guts of the program, is this a reasonable way to write the code to strip tabs from a text file? For writing the output file, this seems to work in the interpreter: g = open('temp.txt', 'w') g.writelines(map(detab, f.xreadlines())) g.close() In practice, does this avoid creating the whole string in memory at one time, as is done by using ''.join()? (I'll have to read up on "opaque sequence objects", which have only been mentioned once or twice in passing - another instance perhaps being an xrange()?) Not that that matters much in practice (in this simple case), but it seems elegant to avoid creating the whole output file at once. OK, I'm just getting my feet wet, and I'll try not to ask too many silly questions! First impressions are: (1) Python seems both elegant and practical; and (2) Beazley seems a pleasantly unfussy introduction for someone with at least a little programming experience in other languages. -- Angus Rodgers -- http://mail.python.org/mailman/listinfo/python-list