Easier way to do this?

20/20 Lab lab at 2020fresno.com
Wed Oct 4 17:31:41 EDT 2017



On 10/04/2017 01:55 PM, Ben Bacarisse wrote:
> 20/20 Lab <lab at 2020fresno.com> writes:
>
>> Looking for advice for what looks to me like clumsy code.
>>
>> I have a large csv (effectively garbage) dump.  I have to pull out
>> sales information per employee and count them by price range. I've got
>> my code working, but I'm thinking there must be a more refined way of
>> doing this.
> I second the suggestion to use the CSV module.  It's very simple to use.
>
>> ---snippet of what I have---
>>
>> EMP1 = [0,0]
>> EMP2 = [0,0]
>> EMP3 = [0,0]
>>
>> for line in (inputfile):
>>      content = line.split(",")
>>      if content[18] == "EMP1":
>>          if float(content[24]) < 99.75:
>>              EMP1[0] += 1
>>          elif float(content[24]) > 99.74:
>>              EMP1[1] += 1
>>      if content[18] == "EMP2":
>>          if float(content[24]) < 99.75:
>>              EMP2[0] += 1
>>          elif float(content[24]) > 99.74:
>>              EMP2[1] += 1
>>      if content[18] == "EMP3":
>>          if float(content[24]) < 99.75:
>>              EMP3[0] += 1
>>          elif float(content[24]) > 99.74:
>>              EMP3[1] += 1
>>
>> and repeat if statements for the rest of 25+ employees.
> Eek!  When you have named objects selected using a string that is the
> object's name you know you want a dict.  You'd have a single dict for
> all employees, keyed by the tag in field 18 of the file.  Does that
> help?
>
> I'm deliberately not saying more because this looks like a learning
> exercise and you probably want to do most of it yourself.
>
> <snip>

Looks like I'll be going with the CSV module.  Should trim it up nicely.

It's not quite a 'learning exercise', but I learn on my own if I treat 
it as such.  This is just to cut down a few hours of time for me every 
week filtering the file by hand for the office manager.

Thanks for the pointers,
Matt



More information about the Python-list mailing list