2

I have a CSV file that I want am reading as a configuration file to create a list of lists to store data in.

The format of my CSV file is:

list_name, search_criteria  
channel1, c1  
channel2, c2  
channel3, c3

I want to read in the CSV file and dynamically create the list of lists from the list_name data as it could grow and shrink over time and I always want whatever is defined in the CSV file.

The list_name in the CSV file is a "prefix" to the list name i want to create dynamically. For example, read in "channel1", "channel2", "channel3" from the csv file and create a lists of lists where "mainList[]" is the core list and contains 3 lists within it named "channel1_channel_list", "channel2_channel_list", "channel3_cannel_list".

I realize my naming conventions could be simplified so please disregard. I'll remain once i have a working solution. I will be using the search criteria to populate the lists within mainLists[].

Here is my incomplete code:

mainList = []

with open('list_config.csv') as input_file:
    dictReader = csv.DictReader(input_file)

    for row in dictReader:
        listName = row['list_name'] + '_channel_list'
2
  • 2
    Don't create a list of lists, create a dict of lists, using the names the keys. Commented Nov 1, 2017 at 19:29
  • Thank you. I am a python newbie and am not sure how to do that. Can you offer an example? Commented Nov 1, 2017 at 19:33

2 Answers 2

2

Here's how to read your data and create a dict from it.

As well as reading data from files, the csv readers can read their data from a list of strings, which is handy for example code like this. With your data you need to specify skipinitialspace=True to skip over the spaces after the commas.

import csv

data = '''\
list_name, search_criteria
channel1, c1
channel2, c2
channel3, c3
'''.splitlines()

dictReader = csv.DictReader(data, skipinitialspace=True)

main_dict = {}
for row in dictReader:
    name = row['list_name'] + '_channel_list'
    criteria = row['search_criteria']
    main_dict[name] = criteria

# Print data sorted by list_name
keys = sorted(main_dict)
for k in keys:
    print(k, main_dict[k])

output

channel1_channel_list c1
channel2_channel_list c2
channel3_channel_list c3

This code is a little more complicated than Joe Iddon's version, but it's a little easier to adapt if you have more than two entries per row. OTOH, if you do only have two entries per row then you should probably use Joe's simpler approach.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 For the clever skipinitialspace, I have only used the csv module a couple of times, and that is a cool trick (and of course the mention of me :))
1

Load in the file in a with statement, then use csv.reader which returns a reader object. This can then be converted to a dictionary by passing it into dict():

with open('list_config.csv') as input_file:
    dictionary = dict(csv.reader(input_file))

Now, the contents of dictionary is:

{'channel3': ' c3', 'channel1': ' c1', 'channel2': ' c2'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.