Callum Twig
Callum Twig

Reputation: 65

Searching columns in a csv file

I have a program which generates a random name and a random question (both independent from eachother). I have created a function called QuestionGenerator() , and it should search through the second column of a csv file and write all the values to the array.

def QuestionGenerator():
    questionlist_file = open('StudentNames&Questions.csv')
    reader = csv.reader(questionlist_file)
    rownum=0
    array=[]
    for row in reader:
        array.append(row)
        rownum=rownum+1
    i = random.randint(0,3)
    question = array[0,1]
    return question

Currently it writes all the values in the file to the array, and not only the second column (question column). So the array should contain the following values

array = ["Consequences of Devolution", "Sources of the Constitution"...."Reasons for a democratic deficit"]

Here is a picture of the csv file

Please note the csv file is an excel spreadsheet saves as a .csv

Upvotes: 2

Views: 4869

Answers (2)

willk
willk

Reputation: 3817

Pandas might be easier for reading in csv files into a DataFrame, but if you want to go with csv:

Each row that you read in array.append(row) has two columns. To get only the second column, modify the code to read array.append(row[1]) to subset to the correct column.

The array will actually be a list of questions, and to get a random question, you will need to select only one element from the list:

i = random.randint(1,3) question = array[i]

Notice that i should be between 1 and the number of questions because the first entry in array will be "questions", the name of the column. To accomplish this, we can use i = random.randint(1, len(array) - 1) which can handle different numbers of questions.

Complete working code is:

def QuestionGenerator():
    questionlist_file = open('StudentNames&Questions.csv')
    reader = csv.reader(questionlist_file)
    rownum=0
    array=[]
    for row in reader:

        # Check to make sure the question cell is not blank
        if row[1] != '':

            # Add only the second column to list
            array.append(row[1])
            rownum=rownum+1

    # Select a random question from the list
    i = random.randint(1,len(array) - 1)
    question = array[i]

    return question

Upvotes: 4

pragmaticprog
pragmaticprog

Reputation: 570

Your code reads an entire row and saves the value to the array. You could look into using either pandas or CSV reader for saving only targeted columns.

If using pandas, the structure would look something like this:

import pandas as pd
df = pd.read_csv(csv_file)
column_of_interest = df.name_of_column #or df['name_of_column']

Using CSV library:

included_cols = [2]
for row in reader:
    column_of_interest = list(row[i] for i in included_cols) #included_cols could be a list or single value

Hope this helps.

Upvotes: 1

Related Questions