The.
The advantage of using this script is that it leaves spaces that were
within double quotes unchanged.
There are no command-line arguments for this script. The script will
prompt the user for source and destination file information.
You can redefine the variables for the original and new delimiters,
delim and new_delim, in the
script as needed.
#!/usr/local/bin/python
import os
print """ Change text file delimiters.
# Ask user for source and target files.
sourcefile = raw_input('Please enter the path and name of the source file:')
targetfile = raw_input('Please enter the path and name of the target file:')
# Open files for reading and writing.
source = open(sourcefile,'r')
dest = open(targetfile,'w')
# The variable 'm' acts as a text/non-text switch that reminds python
# whether it is working within a text or non-text data field.
tswitch = 1
# If the source delimiter that you want to change is not a space,
# redefine the variable delim in the next line.
delim = ' '
# If the new delimiter that you want to change is not a tab,
# redefine the variable new_delim in the next line.
new_delim = '\t'
for charn in source.read( ):
if tswitch = = 1:
if charn = = delim:
dest.write(new_delim)
elif charn = = '\"':
tswitch = tswitch * -1
else:
dest.write(charn)
elif tswitch = = -1:
if charn = = '\"':
tswitch = tswitch * -1
else:
dest.write(charn)
source.close( )
dest.close( )
Use of redelim.py assumes that you have
installed Python, which is available through the ports collection or
as a binary package. The Python module used in this code is installed
by default.
O'Reilly Home | Privacy Policy
© 2007 O'Reilly Media, Inc.
Website:
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.