parsing csv files class

Gary M. Josack gary at byoteki.com
Sat Dec 27 14:02:13 CET 2008


alex goretoy wrote:
> I know it's messy with all those self.soc.* functions, but it works in 
> one of my current project. I just want to make it more pythonic I also 
> want to add capability for makeing csv file if I give it input like:
> 1234,something nice, hey this is something nice
> 2468,something else, something else
>
> On Sat, Dec 27, 2008 at 4:54 AM, alex goretoy 
> <aleksandr.goretoy at gmail.com <mailto:aleksandr.goretoy at gmail.com>> wrote:
>
>     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
>     -- 
>     А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
>     а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
>
>
>
>
> -- 
> А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
> а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
> ------------------------------------------------------------------------
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>   
Do you know that there is a csv module in the standard library already?

Thanks,
Gary M. Josack



More information about the Python-list mailing list