搜索csv文件中的列

时间:2018-06-25 11:53:35

标签: python csv row col

我有一个程序,该程序生成一个随机名称和一个随机问题(彼此独立)。我创建了一个名为QuestionGenerator()的函数,它应该在csv文件的第二列中搜索并将所有值写入数组。

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

当前,它将文件中的所有值写入数组,而不仅仅是第二列(问题列)。因此数组应包含以下值

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

Here is a picture of the csv file

请注意,csv文件是一个另存为.csv的excel电子表格

2 个答案:

答案 0 :(得分:2)

Pandas可能更容易将csv文件读入DataFrame,但是如果要使用csv

您在array.append(row)中读取的每一行都有两列。要仅获取第二列,请修改代码以读取array.append(row[1])以将其子集到正确的列。

array实际上是一个问题列表,要获得随机问题,您只需从列表中选择一个元素:

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

请注意,我应该在1到问题数之间,因为array中的第一个条目将是“问题”,即列的名称。为此,我们可以使用i = random.randint(1, len(array) - 1)来处理不同数量的问题。

完整的工作代码是:

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

答案 1 :(得分:1)

您的代码读取整行并将值保存到数组。您可以考虑使用pandas或CSV阅读器来仅保存目标列。

如果使用熊猫,结构将如下所示:

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

使用CSV库:

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

希望这会有所帮助。

相关问题