Python CSV文件,从CSV文件创建列表,并执行数据搜索

Python CSV文件,从CSV文件创建列表,并执行数据搜索,python,csv,Python,Csv,我正在为我的任务制作这个程序,我被支持搜索是否有预先注册的玩家出现在列表中,查找特定玩家的号码,打印玩家列表及其信息,并使用至少一个if-else或elif语句。我通过导入“battle_royale.csv”文件来访问数据 支持代码的输出如下所示: 到目前为止,我只有这些: def main(): avatarNames = [”LarchDew15”,”Pinerain2”,”xOakenMaidx”,”Grandidel”,”Gened123”,”Tufty98”,”si

我正在为我的任务制作这个程序,我被支持搜索是否有预先注册的玩家出现在列表中,查找特定玩家的号码,打印玩家列表及其信息,并使用至少一个if-else或elif语句。我通过导入“battle_royale.csv”文件来访问数据

支持代码的输出如下所示:

到目前为止,我只有这些:


def main():
   
   avatarNames = [”LarchDew15”,”Pinerain2”,”xOakenMaidx”,”Grandidel”,”Gened123”,”Tufty98”,”silverstar”,”grimRAVEN”,”fogdell”,”111marshglitter111”,
   ”1337Vale”,”pinesword”,”GreyLore”,”silveneye90””Shaewaith1999”,”ronar”,”yulnul”,”durowen”,”glyrgrim”,”Goghyll55”,
   ”Welriel21”,”Glanros0000”,”Lochach2000”,”Ashioth”,
   ”ashrar12”,”immain_321”,”kwelnar”,”Talzak01”,”Lirzen”,”Yoraish555",
   ”Renryl”,”ghuluith000”,”ryzenfire”,”gryffenford”,”collock”,
   ”sidwick2005”,”fayrewater”,”beestelonde”,”mucktor1x1”,”dwalegarth”,
   ”namankol”,”qigomx0x”,”Iderdizan2001”,”bulbascore100”,”enaux0x0x0”,
   ”yojugo1001”,”sayeon121”,”yabu111”]
   playerNames = [”Emily”,”Hannah”,”Madison”,”Jacob”,”Micheal”,”Matthew”,”Ashley”,”Sarah”,”Christopher”,”Alexis”,”Nicholas”,”Samantha”,
 ”Andrew”,”Javier”,”Caleb”,”Hunter”,”Nicholas”,”Samantha”,”Andrew”,
”Jessica”,”Taylor”,”Daniel”,”Tyler”,”Joshua”,”Elizabeth”,”Billy”,”Olivia”,”Ethan”,”Abigail”,”Emma”,“Alexander”,”Isabella”,”Sophia”,”Xavier”,“Maya”,”Landon”,”Owen”,”Devin”,“Jocelyn”,“Diego”,
“Cody”,”Damian”,”Zoey”,”Sadie”,”Travis”,”Eli”,”Colin”,“Braden”,”Quinn”,”Conner”,”Cassidy”,
”Riley”,”Morgan”,”Javier”,”Caleb”,”Hunter”]
   playerNumber = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50]
   
   print(”Welcome to the Battle Royale Game Tournament Registration”)
   print(”  ****************Main Menu****************”)
   options = input(”A: find pre-registered player ; B: Find the number of a specific player ; C: Print lists of player ; Q: Quit/Log out   Please enter your choice:”)
   
main()
我建议使用,这里不能出错:

将熊猫作为pd导入
df=pd.read_csv(“path/to/file.csv”,columns=[“化身名称”,“玩家编号”])
如果df[“化身名称”].isin([化身名称]):
做某事
但是,您也可以使用该模块

导入csv
数据=[]
打开('file.csv',换行符='')作为csvfile:
dataset=csv.reader(csvfile,分隔符='',引号='|')
对于数据集中的行:
data.append(行)
用数据做东西

我的python脚本所在的目录中有一个csv文件。这是工作表的屏幕截图:

excel文件的名称为test_book.csv

如果我使用以下代码:

import csv

def main():
   with open('test_book.csv') as csv_file: # Opens the book
       csv_reader = csv.reader(csv_file, delimiter=',')

       for idx, row in enumerate(csv_reader): # Looping through each line with a value
           if idx > 0:
               print("The username is %s, the name is %s and the number is %s" %(row[0], row[1], row[2]))
它给了我:

如果我正在寻找用户,我可以使用以下代码:

import csv

def main():
    user_choice = input("Please give the number of the user you wish to search for: ")
    with open('test_book.csv') as csv_file: # Open the book
        csv_reader = csv.reader(csv_file, delimiter=',') # Read the file

        for row in csv_reader: # Loop through the book
            if row[2] == user_choice: # Row[2] is the third column along - since indexes start at 0
                print("The username is %s, the name is %s and the number is %s" %(row[0], row[1], row[2]))
现在我得到:


您想问一个关于您的代码的问题吗?是的,我将如何在主菜单中的每个选项中使用if和elif语句当您尝试使用
if
elif
时出现了什么问题?我不确定如何将其用于csv文件。如果用户从主菜单中选择了A,我不确定如何告诉程序搜索预注册的播放器。如果您开始编写一个程序,当用户输入“A”时打印“您选择了A”,当用户输入“B”时打印“您选择了B”,当用户输入任何其他内容时打印“您没有选择”。暂时忘记CSV文件和播放机号码,直到它起作用。