你如何在python中搜索CSV文件?

时间:2015-11-15 17:47:30

标签: python csv python-3.2

我正在尝试创建一个包含两个标题的CSV文件:

Name Score

然后输入名称和分数。这样,如果程序再次运行,它会发现名称和分数已经实现,而不是再次运行,而是跳过该过程,然后继续输入名称和分数。这是我目前的代码如下。任何建议或帮助我非常欢迎:

filename = Class_Name + ".csv" #have already defined filename earlier

def excel_search():
    with open(filename, 'rb') as file_data:
        reader = csv.DictReader(file_data, ("Score"))
        if i not in reader:
            with open(filename, 'w', newline='') as f:
                data= "Name","Score"
                f.write(str(data))



excel_search()
    with open(filename, 'a', newline='') as f:
        f.writerows('\n'[Student_Name],[User_Score]) # have defined userscore and studentname earlier

1 个答案:

答案 0 :(得分:0)

不确定这是否与您的目的完全相同,但它可能适用于您的目的。

import csv
search=input('Enter string to search: ')
stock=open ('FileName.csv', 'wb')
reader=csv.reader(FileName)
for row in reader:
    for field in row:
        if field==code:
            print('Record found! \n')
            print('Name: '+row[0])
            print('Score: '+row[1])

print('Name: '+row[0])应打印名称,print('Score: '+row[1])应打印播放器的分数。