[SPAM] It's ...

MRAB python at mrabarnett.plus.com
Wed Jun 24 16:44:27 EDT 2009


Angus Rodgers wrote:
[snip]
> 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()
> 
stop = 3   # Tab stops every 3 characters
detab = lambda s: s.expandtabs(stop)
f = open('h071.txt')   # Do some stuff to f, perhaps, and then:
# f.seek(0) # Not necessary
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.
> 
STOP = 3  # Tab stops every 3 characters
in_file = open('h071.txt')
out_file = open('temp.txt', 'w')
for line in in_file:  # Iterates one line at a time
     out_file.write(line.expandtabs(STOP))
in_file.close()
out_file.close()



More information about the Python-list mailing list