[Tutor] Collating date data from a csv file

Dave Hill dave at the-hills.org.uk
Thu May 9 09:06:43 EDT 2019


Thank you, I now have

defaultdict(<class 'int'>, {736697: 10, 736677: 14, 736980: 9, 737109: 
50, 736919: 15, 736652: 19, 736502: 14, 736710: 2, 736674: 6, 736672: 5, 
736933: 2, 736932: 6, 736658: 7, 736671: 5, 736499: 6, 736707: 4, 
737181: 4, 736686: 2, ...

where the first number is the proleptic Gregorian ordinal of the date 
8-) ( I had to look up what proleptic meant)

This means I can access the elements by the ordinal of the date, for 
later processing, and extraction to a spreadsheet

Dave


On 09/05/2019 04:08, Cameron Simpson wrote:
> On 08May2019 21:04, Dave Hill <dave at the-hills.org.uk> wrote:
>> I have a csv file which details the results of equipment tests, I 
>> carry out PAT testing as a volunteer at a heriatge railway in N. 
>> Wales. I want to extract how many items were tested on each test day. 
>> So far I have generated a List of test dates, but I am now stalled at 
>> how to efficiently count numbers tested on each date.
>>
>> Can I have a list of tuples, where one item is the date and the 
>> second the count?
>
> Not as such, because you can't modify a tuple (so you can't update the 
> count part). But you could use a 2 element list.
>
>> or is there a better construct?
>
> Oh definitely. The easiest thing would be a defaultdict(int). Example:
>
>  from collections import defaultdict
>  ...
>  by_date = defaultdict(int)
>  for row in csvdata:
>    timestamp = row[1]  # based on your example data
>    # get the date from the timestamp
>    date = ...
>    by_date[date] += 1
>
> A defaultdict is a dict which magicly makes missing elements when they 
> get access, using a factory function you supply. Here we're using 
> "int" as that factory, as int() returns zero.
>
> I presume you've got the timestamp => date conversion sorted?
>
> Cheers,
> Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list