Python Forum
match type with value in csv parsing function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
match type with value in csv parsing function
#1
Hello,

In the following code I do some parsing of files (csv):
#!/usr/bin/env python3

import csv

def parse_csv(filename, select=None, types=None):
    '''
    Parse a CSV file into a list of records
    '''
    with open(filename) as f:
        rows = csv.reader(f)

        # Read the file headers
        headers = next(rows)
        if select:
            indices = [ headers.index(colname) for colname in select ]
            headers = select
        else:
            indices = []

        records = []
        for row in rows:
            if not row:    # Skip rows with no data
                continue
            # Filter the row if specific columns were selected
            if indices:
                row = [ row[index] for index in indices ]
            if types:
                row = [ func(row) for func, val in zip(types, row) ]

            record = dict(zip(headers, row))
            records.append(record)

    return records

shares_held = parse_csv('../Data/portfoliodate.csv', select=['name', 'time'], types=[str, str])
print(shares_held)
My question is related to this sentence:
row = [ func(row) for func, val in zip(types, row) ]
Is matching types with row essential here? If I replace it with this:
row = [ func(row) for func in types ]
the code seems to be behave identically, because you have to match the type and values anyway when calling the function (types=[str, str] ), otherwise it errors out.

Looking forward to your answers :)
Reply
#2
What I've understood thus far is that this wouldn't/shouldn't work:
row = [ func(row) for func in types ]
I am applying the function (str) to the entire list and it doesn't make sense. Whereas iterating over the list should work.

The reason why I thought it was behaving differently was that I didn't actually test it correctly. Applying str on a list works, but applying anything else doesn't. And I was trying to test it by changing str to int (for a date format), and it didn't work and I thought this was expected behaviour, but I hadn't properly test int/float which wouldn't have worked either way.
Reply
#3
please post sample (small) csv file
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  determine parameter type in definition function akbarza 1 586 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  i want to use type= as a function/method keyword argument Skaperen 9 1,860 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Creating function inside classes using type wolfmansbrother 3 2,334 Mar-20-2020, 01:21 AM
Last Post: wolfmansbrother
  Type hinting - return type based on parameter micseydel 2 2,485 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Getting type from input() function in Python 3.0 leodavinci1990 7 3,745 Jul-29-2019, 08:28 PM
Last Post: avorane
  Type function does not work sunnyarora 2 2,485 Mar-15-2019, 10:50 AM
Last Post: sunnyarora
  Should a function ever be more permissive than its type hints? Shay 1 1,942 Mar-13-2019, 05:36 PM
Last Post: Larz60+
  Parameters type in a function girbinho 2 2,739 Oct-09-2018, 10:36 PM
Last Post: micseydel
  function type? microphone_head 3 3,342 Sep-13-2018, 05:44 AM
Last Post: vijayhackr
  Why args type is always tuple, when passed it as argument to the function. praveena 5 5,329 Jan-16-2018, 09:07 AM
Last Post: praveena

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020