简体   繁体   English

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

[英]How do you search a CSV file in python?

I am trying to create a CSV file with two headings: 我正在尝试创建带有两个标题的CSV文件:

Name Score

and then underneath input the name and score. 然后在下面输入名称和分数。 this is so that if the program was run again it would find that name and score was already implemented and not run it again and instead skip that process and just proceed to enter the name and score. 这样一来,如果再次运行该程序,它将发现该名称和分数已经实现,而不再运行,而是跳过该过程,然后继续输入该名称和分数。 here is my current code below. 这是我下面的当前代码。 any advice or help i greatly welcome: 我非常欢迎任何建议或帮助:

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

Not sure if this is exactly what you are going for but it may work for your purpose. 不确定这是否正是您要使用的功能,但可能可以满足您的目的。

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])

The print('Name: '+row[0]) should print the name and the print('Score: '+row[1]) should print the player's score. print('Name: '+row[0])应该打印名称,而print('Score: '+row[1])应该打印玩家的分数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM