0

I am trying to do something that seems straightforward, but is giving me endless trouble.

What I would like to do:

1 for i in nameList 2 Iterate through each row of aggregatedCSV 3 If i is a partial match in current row, append that entire row to a new name-specific CSV (repeat steps 2 and 3 for remaining i in nameList) 

nameList = ['Jon', 'Bob', 'Tim']

aggregatedCSV = [ [1, '3', 'Bob85'], [2, 'Jon52', '8'], ['Bob1', '14', 3], ['Tim95', 8, '6'], ['8', 11, 'Tim48'], [10, 'Jon11', '44'], [26, '21', 'Jon90'], [99, '23', 'Bob19'], [7, '24', 'Tim82'] ] 

The desired output would ultimately be three new CSV files but, to keep it simple for here, I am trying to get something like:

JonList = [[2, 'Jon52', '8'], [10, 'Jon11', '44'],[26, '21', 'Jon90']]

BobList = [[1, '3', 'Bob85'], ['Bob1', '14', 3], [99, '23', 'Bob19']]

TimList = [['Tim95', 8, '6'], ['8', 11, 'Tim48'], [7, '24', 'Tim82']]

Although I have manually created nameList for this example, I will be reading from csv files that will have an unknown number of rows, with an unknown number of values per row.

Any help is appreciated.

1 Answer 1

1

I don't know python so there is surely a faster, more efficient way but this is what I came up with:

from collections import defaultdict nameSpecificData = defaultdict(list) for name in nameList: for row in aggregatedCSV: for item in row: if name in str(item): nameSpecificData[name].append(row) 

This stores the results in a dictionary keyed on the name so that you don't need to know what is in the nameList in order to make output variables:

When run with your input, it results in:

 { 'Jon': [[2, 'Jon52', '8'], [10, 'Jon11', '44'], [26, '21', 'Jon90']], 'Bob': [[1, '3', 'Bob85'], ['Bob1', '14', 3], [99, '23', 'Bob19']], 'Tim': [['Tim95', 8, '6'], ['8', 11, 'Tim48'], [7, '24', 'Tim82']] } 

If you really, really want to make separate name specific variables, then this will work:

JonList = [] BobList = [] TimList = [] for name in nameList: for row in aggregatedCSV: for item in row: if name in str(item): globals()[name+'List'].append(row) 

And it produces your desired output:

>>> print(JonList) [[2, 'Jon52', '8'], [10, 'Jon11', '44'], [26, '21', 'Jon90']] >>> print(BobList) [[1, '3', 'Bob85'], ['Bob1', '14', 3], [99, '23', 'Bob19']] >>> print(TimList) [['Tim95', 8, '6'], ['8', 11, 'Tim48'], [7, '24', 'Tim82']] 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Jerry Jeremiah. It seems you know plenty of Python to me! I got it outputting to cvs files with your second suggestion and including this before the end of the loop: file = open(folder + 'new/' + name+'List' + '-parsed.csv', 'a', newline='') with file: write = csv.writer(file) write.writerows(globals()[name+'List'])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.