parsing csv files class
alex goretoy
aleksandr.goretoy at gmail.com
Tue Dec 30 22:25:15 EST 2008
This line doesn't work for me. bufferp is empty afterwards.
self.bufferp= [dict(zip(header,line)) for line in reader]
needs to be this
self.bufferp= [dict(zip(header,line)) for line in self.buffer]
after reading from the reader, it becomes empty. Figured maybe someone would
find this info useful. Thank you Tim for helping me make my library better.
I may need some assistance in another thread about a pycurl library. I want
it to fall back to urllib/urllib2 if pycurl is not installed module. look
for thread "pycurl urllib fallback" I will post it here shortly to
python-list.
On Mon, Dec 29, 2008 at 6:15 AM, alex goretoy
<aleksandr.goretoy at gmail.com>wrote:
> Tim, Thank you for your suggestions that you made. I will modify my class
> to what you said. I will also remove find_and_replace. seeing as I won't use
> it anywhere else. I think I put it there for some test and forgot to delete
> it. I was actually deleting the header outside of the class. This works much
> better for me. Any other suggestions are appreciated. Thank you. -A
>
>
> On Sun, Dec 28, 2008 at 5:46 AM, Tim Roberts <timr at probo.com> wrote:
>
>> "alex goretoy" <aleksandr.goretoy at gmail.com> wrote:
>> >
>> >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]
>>
>> Style issue: unless you are running Python 1.x, you virtually never need
>> to import the "string" module. Also, you can always refer to the last
>> element of a list or tuple by using [-1]:
>>
>> parts = self.filename.split( "/" )
>> filename = "/tmp/" + parts[-1]
>>
>>
>> > 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
>>
>> You might consider keeping the header line separate.
>>
>> reader = csv.reader(open(filename, "rb"))
>> header = reader.next()
>> self.buffer = list(reader)
>> self.bufferp = [ dict( zip( header, line ) ) for line in reader ]
>> self.header = header
>>
>> Also, you don't really need a separate "total" variable, since it's equal
>> to len(self.buffer).
>>
>> > 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
>>
>> There's a problem here, as this was originally written. "self.total"
>> starts out being a function (this one here). But after self.parse runs,
>> "self.total" will be an integer, and this function is lost. You need to
>> decide whether you want users to just access the self.total integer, or
>> force them to use the function. In the latter case, you can change the
>> counter to self._total.
>>
>> On the other hand, the self.total counter is unnecessary:
>> def total(self):
>> return len(self.buffer)
>>
>> > 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 find_and_replace(self,li,fi,re):
>> return [l.replace(fi,re) for l in li]
>>
>> I'm not sure why this is a member of the class; it doesn't use any of the
>> members.
>> --
>> Tim Roberts, timr at probo.com
>> Providenza & Boekelheide, Inc.
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
>
> --
> А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
> а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
>
--
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081231/f6fb4f27/attachment-0001.html>
More information about the Python-list
mailing list