如何从CSV文件中读取单独的行?

时间:2015-06-14 15:37:29

标签: python csv

目前我已经有了这个功能:

def writer(file_name)
    open_file = open(file_name,"r+", newline='')
    csv_output = csv.writer(open_file) 
    csv_output.writerow(student)
    open_file.close()

其中student是:

"string_1","string_2",int

我希望首先阅读该文件,然后检查我写的"string_1"是否与已编写的任何"string_1"匹配,但我可以'找到一个内置函数,让我读取每一行并将其存储为列表。

1 个答案:

答案 0 :(得分:0)

首先,您必须打开文件进行阅读,逐行浏览文件并返回,如果" string_1"找到了:

def append_student(file_name, student)
    with open(file_name, "r") as f:
        for line in csv.reader(f):
            if line[0] == student[0]:
                return
    with open(file_name, "a") as f:
        csv.writer(f).writerow(student)