![]() |
match type with value in csv parsing function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: match type with value in csv parsing function (/thread-36143.html) |
match type with value in csv parsing function - vinci - Jan-20-2022 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 :) RE: match type with value in csv parsing function - vinci - Jan-21-2022 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. RE: match type with value in csv parsing function - Larz60+ - Jan-21-2022 please post sample (small) csv file |