Unhelpful traceback

John Nagle nagle at animats.com
Thu Mar 7 13:42:56 EST 2013


On 3/7/2013 5:10 AM, Dave Angel wrote:
> On 03/07/2013 01:33 AM, John Nagle wrote:
>> Here's a traceback that's not helping:
>>
> 
> A bit more context would be helpful.  Starting with Python version.

    Sorry, Python 2.7.

> 
> If that isn't enough, then please give the whole context, such as where
> zipelt and filename came from.  And don't forget to specify Python
> version.  Version 3.x treats nonbinary files very differently than 2.x

     Here it is, with some email wrap problems.

				John Nagle


    def dofilecsv(self, infilename, infdraw) :
        """
        Loader for Companies House company data, with files already open.
        """
        self.logger.info('Converting "%s"' % (infilename, ))        # log
        (pathpart, filepart) = os.path.split(infilename)            #
split off file part to construct outputfile)
        (outfile, ext) = os.path.splitext(filepart)                 #
remove extension
        outfile += ".sql"                                           #
add SQL suffix
        outfilename = os.path.abspath(os.path.join(self.options.destdir,
outfile))
        #   ***NEED TO INSURE UNIQUE OUTFILENAME EVEN IF DUPLICATED IN
ZIP FILES***
        decoder = codecs.getreader('utf-8')                         #
UTF-8 reader
        with decoder(infdraw,errors="replace") as infd :
            with codecs.open(outfilename, encoding='utf-8', mode='w') as
outfd :
                headerline = infd.readline()                        #
read header line
                self.doheaderline(headerline)                       #
process header line
                reader = csv.reader(infd, delimiter=',', quotechar='"')
# CSV file
                for fields in reader :                              #
read entire CSV file
                    self.doline(outfd, fields)                      #
copy fields
        self.logstats(infilename)                                   #
log statistics of this file

    def dofilezip(self, infilename) :
        """
        Do a ZIP file containing CSV files.
        """
        try :
            inzip = zipfile.ZipFile(infilename, "r", allowZip64=True)
# try to open
            zipdir = inzip.infolist()                       # get
objects in file
            for zipelt in zipdir :                          # for all
objects in file
                self.logger.debug('ZIP file "%s" contains "%s".' %
(infilename, zipelt.filename))
                (infile, ext) = os.path.splitext(zipelt.filename) #
remove extension
                if ext.lower() == ".csv" :                   # if a CSV file
                    with inzip.open(zipelt.filename,"r") as infd :
      # do this file
                        self.dofilecsv(infile, infd)        # as a CSV file
                else :
                    self.logger.error('Non-CSV file in ZIP file: "%s"' %
(zipelt.filename,))
                    self.errorcount += 1                    # tally

        except zipfile.BadZipfile as message :              # if trouble
            self.logger.error('Bad ZIP file: "%s"' % (infilename,))  #
note trouble
            self.errorcount += 1                            # tally

    def dofile(self, infilename) :
        """
        Loader for Companies House company data
        """
        (sink, ext) = os.path.splitext(infilename) # get extension
        if ext == ".zip" :   # if .ZIP file
            self.dofilezip(infilename)                  # do ZIP file
        elif ext == ".csv" :
            self.logger.info('Converting "%s"' % (infilename,))# log
            with open(infilename, "rb") as infd :
                self.dofilecsv(infilename, infd)        # do
            self.logstats(infilename)                   # log statistics
of this file
        else :
            self.logger.error('File of unexpected type (not .csv or
.zip): %s ' % (infilename,))
            self.errorcount += 1






More information about the Python-list mailing list