parsing csv files class

alex goretoy aleksandr.goretoy at gmail.com
Sat Dec 27 11:54:59 CET 2008


Hello All,

I have this class that I use in one of my projects. I know it's missing
functionality and some things could have been done differently. Can you ehlp
me make this class better? What can I do to make it more resistant to error?
You can find the stdout_colours class on Google if you want it, JFGI I want
to make it more pythonic. I come from a PHP background, can you tell?

Any and all help is appreciated
-Alex

#!/usr/bin/env python
from ctypes import *
import os, sys, types, csv, urllib, urllib2, urlparse, string,stdout_colours

class parsercsvy(object):
    """Return a line from a csv file or total amount of lines"""
    def __init__(self,file_name=""):
        self.func_me_color="white_on_black"
        self.soc=stdout_colours.stdout_colors()
        self.soc.me_him(['ENTER:',__name__],self.func_me_color)
        self.filename = file_name
        self.buffer = []
        self.bufferp= []
        if string.find(self.filename,"http") != -1:
            resp=urllib2.urlopen(self.filename)
            file=resp.read()
            lfi=len(string.split(self.filename,"/"))
            filename = "/tmp/"+string.split(self.filename,"/")[lfi-1]
            f=open(filename,"w")
            f.write(file)
            f.close
            self.parse(self.filename)
        else:
            self.parse(self.filename)
        self.soc.me_him(['EXIT:',__name__],self.func_me_color)
    def parse(self,filename,ret=0):
        self.soc.me_him(['ENTER:',__name__],self.func_me_color)
        i = 0
        try:
            reader = csv.reader(file(filename, "rb"))
            try:
                for row in reader:
                    self.buffer.append(row)
                    s,a=[],{}

                    for j in range(len(self.buffer[0])):
                        a[self.buffer[0][j]]=row[j]
                    self.bufferp.append(a)
                    i+=1
                self.total = i-1
            except csv.Error, e:
                sys.exit('file %s, line %d: %s' % (filename,
reader.line_num, e))
        except IOError, e:
            sys.exit('file %s, IOError: %s' % (filename, e))
        self.soc.me_him(['EXIT:',__name__],self.func_me_color)
    def index(self, index):
        """return line for index"""
        self.soc.me_him(['ENTER:',__name__],self.func_me_color)

self.soc.me_him(['RETURN:',self.buffer[int(index)],__name__],self.func_me_color)
        return self.buffer[int(index)]
    def total(self):
        """return total number of lines in csv file"""
        self.soc.me_him(['ENTER:',__name__],self.func_me_color)
        self.soc.me_him(['RETURN:',self.total,__name__],self.func_me_color)
        return self.total
    def header(self):
        """return csv header == line 0"""
        self.soc.me_him(['ENTER:',__name__],self.func_me_color)

self.soc.me_him(['RETURN:',self.buffer[0],__name__],self.func_me_color)
        return self.buffer[0]
    def find_and_replace(self,li,fi,re):
        """
        find and replace a string inside a string, return list
        find_and_replace(list,find,replace)
        """
        this=[]
        for l in li:
#            found_index=string.find(l,fi)
            this.append(l.replace(fi,re))
        return this
    def return_buffer(self):
        self.soc.me_him(['ENTER:',__name__],self.func_me_color)
        self.soc.me_him(['RETURN:',self.buffer,__name__],self.func_me_color)
        return self.buffer
if __name__ == "__main__":
    if len(sys.argv) < 1:
        print "Usage: %s file"% sys.argv[0]
    f=sys.argv[1]
    c=csv_parser(f)
    print c.bufferp
-- 
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081227/68a533d6/attachment.html>


More information about the Python-list mailing list