Problem with a tuple - newbie ignorance

Roland Schlenker rol9999 at attglobal.net
Mon Jan 15 16:41:53 EST 2001


Steven Citron-Pousty wrote:
> 
> Sorry all about my incomprehensible message. Honestly, I tried reading
> the woodrat and the alligator but I can't get this too work.
> I didn't want to send code because I thought everyone would get POed at
> me sending code.
> Here is my code - remember think newbie and don't slam me too hard, its
> one of those days. Thanks again for any help
> Steve
> 
> import os
> import sys
> import string
> import re
> 
> #numfields is the number of fields to potentially parse
> numfields = 41
> 
> """  THIS LIST IS INCOMPETE - get complete list from the spreadsheet
>    ifields[0] is the name of the field
>     ifields[1] is the content of that field
>    so if we add a new item to the fields we have to add '' to the other
> 2 lists
>    """
> ifields = [['ID =', 'T = ', 'AU =', 'DIST =', 'DNUM =', 'ABS =',
> 'ARCH.FILTER = </B>I', 'ARCH.FILTER = </B>C', 'ARCH.FILTER = </B>N',
> 'ARCH.FILTER = </B>S', 'ARCH.FILTER = </B>E', 'CLASSIF',
> 'ICPSR.CLASSIF1', 'NACJD.CLASS', 'NACDA.CLASS' ,'SAMHDA.CLASS',
> 'IAED.CLASS', 'EXTENT.COLLECT', 'CLASSNO', 'SERIES.NAME', 'SERIES.INFO',
> 'RESTRICTIONS =', 'DATA.TYPE', 'TIME.PERIOD', 'DATE.OF.COLLECT',
> 'FUNDING.AGENCY', 'GRANT.NUMBER', 'DATA.SOURCE', 'EXTENT.PROCESS',
> 'DATA.FORMAT', 'COLLECT.NOTE', 'SAMPLING =', 'UNIVERSE =',
> 'RELATED.PUBS', 'CITATION =', 'KEYWORDS =', 'DIR =', 'CHAPTER =',
> 'SECTION =', 'SUBSECTION =','SUBSUB'],
> ['','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','']]
> 
> #read the files and set up file for writing
> dir = os.listdir('D:\\statlab\\ssda\\data') #read in the list of files
> in this directory
> 
> """open the file"""
> for f in dir:
>     try:
>         fileproc = open('D:\\statlab\\ssda\\data\\'+f, 'r')
>     except IOError:
>         print 'Can\'t open file for reading.'
>         sys.exit(0)
> 
> #create a new iflieds to store the data. More important for looping
>     #through the directory
> 
>     result = ifields[:]
                     ^^^
I think you want a copy of ifields. A list is mutable.

> 
>     #read the file into a list
>     text = fileproc.readlines()
> 
>     #loop through and find an occurnce of a tag
>     #if you find a tag write it to the field
>     #if you don't find a tag write it to the previous found field
>     for i in text:
>         jindex = 0
>         found = 0
>         for j in result[0]:
>             if (i.rfind(j)==0) or (i.rfind(j)==1) or (i.rfind(j)==3):
>                if i.rfind("=")+2 == ' ':
>                    where = i.rfind("=")+3
>                else:
>                    where = i.rfind("=")+2
>                result[1][jindex] = i[where:-1]
>                oldindex = jindex
>                found = 1
>     # need to write a test for ; at the end
>                if result[1][jindex][-1] == ";":
>                    result[1][jindex] = i[where:-2]
>                break
>             elif (i.rfind(j)==6) or (i.rfind(j)==7):
>                if i.rfind("=")+6 == ' ':
>                    where = i.rfind("=")+7
>                else:
>                    where = i.rfind("=")+6
>                result[1][jindex] = i[where:-1]
>                found = 1
>                oldindex = jindex
>                if result[1][jindex][-1] == ";":
>                    result[1][jindex] = i[where:-2]
>                break
>             jindex += 1
>             if i.rfind("BV =") != -1:
>                break
>         if (found != 1) and (i.rfind("BV =") != -1):
>             result[1][oldindex] += i[0:-1]
>             found = 0
> 
>     crap  = 0
>     while crap < numfields :
>         if result[1][crap]:
>             print "field # ", result[0][crap], " ", result[1][crap]
>         crap += 1
> 
> ___________________________________________________________________________________________________

Roland Schlenker



More information about the Python-list mailing list