ASCII delimited files
wjk at wjk.mv.com
Mon Nov 15 21:56:47 EST 1999
Roger Irwin wrote:
> Is there any function or module available for parsing ASCII delimited files,
> before I go and re-invent the wheel writing my own.
> BTW, I know it is simple to do, but I was thinking along the lines that a
> 'standard' could easily be substituted for a C version for large files.
#Was reading news and noted this discussion#on comma-delimited files so I
thought that maybe as a
#programming/python newbie I could try and contribute.
#CVT - comma delimited file input function
#read in lines of input - 1 line =='s 1 record
#pass line to function and return a list of fields
#double quotes are supposed to be in pairs (Beware cause some
#will backslash double quotes and put them in a data field) or
#data could be invalid because of a missing "
#may want to re.compile(pattern) and substitute this combo
#to something else.
#also note that it is also not uncommon to see a tab or comma
#delimited file with other character delimiters in a field
#does seem a bit 'python_awkish' -- I apologize
from string import split
def comma_Cvt( aString ): #function to read cvt string
stop_N_go = 0 #create a guardian
newstr = "" #new string to
for s_Char in aString: #for loop for chars in
if ( s_Char == '\"' ): #Look for " in
s_Char (character but really a string)
if ( stop_N_go == 0 ): #Set guardian to 1 or 0
stop_N_go = 1
continue #if 1st " guardian
to 1; cont.
stop_N_go = 0 #if 2nd " guardian to
if ( s_Char == ','): #if a , as char
if ( stop_N_go == 0 ): #if guardian is 0 set
s_Char = '~~~' #to be ~~~ or
some other combo
newstr = newstr + s_Char #build a newstring
return split(newstr,'~~~') #return list broken up for more
I tried this out on a couple of lines to test and see if it works -- seems too
Anyhow, just an idea -- while I learn I might as well take a stab at stuff and
havn't learned objects yet ...... Oh yeah, thanks to all who have been kind
respond to some of my NEWBIE questions.
More information about the Python-list