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

时间:2020-07-19 16:09:14

标签: python csv

我要为自己的任务制作此程序有些麻烦,我想搜索一下是否有预注册的玩家出现在列表中,找到特定玩家的编号,打印玩家列表并它们的信息,并至少使用一个if-else或elif语句。我通过导入“ battle_royale.csv”文件来访问数据。

假定代码的输出如下所示: enter image description here

这是我到目前为止所拥有的:


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

2 个答案:

答案 0 :(得分:0)

我建议使用pandas,在这里不能出错:

import pandas as pd

df = pd.read_csv("path/to/file.csv", columns=["Avatar_Names", "Player Number"])
if df["Avatar_Names"].isin([avatar_name]):
   do_stuff()

但是,您也可以使用CSV模块。

import csv

data = []
with open('file.csv', newline='') as csvfile:
    dataset = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in dataset:
        data.append(row)

do_stuff_with_data()

答案 1 :(得分:-1)

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

Excel Sheet

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

它给了我

enter image description here

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

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

现在我得到了:

enter image description here