根据 Python 中的用户输入显示数组值

Display array values based on user input in Python

我有一个文本文件为我的数组提供数据。我已经使用 csv 导入了它们。我有多个数组,存储诸如人名、地址、电子邮件等数据。我正在尝试根据用户输入打印出单个数组的值。

elif option==3:
        print("Which report would you like to run?")
        time.sleep(1)
        reportmenu()
        while True:
            choice=int(input(" "))
            if choice ==1:
                city_input=raw_input("Please enter a city which you would like to display all students from")
                for row in City:
                    if city_input in City:
                        cityindex=City.index(city_input)
                        print("{} {} {} {} {} {} {}".format(ID[cityindex],Surname[cityindex],FirstName[cityindex],City[cityindex],Phone_Number[cityindex],Gender[cityindex],Email[cityindex]))

所以如果用户输入了城市名称,并且它出现在City数组中,程序可以打印出存储在其他数组中的与用户相关的其他相关信息。我已经尝试过基于用户输入的索引,但这不起作用,我得到以下输出:

001,Surname1,Shaun,18/09/86,Sheffield,012345,Male,shaun@shaun.com
001,Surname1,Shaun,18/09/86,Sheffield,012345,Male,shaun@shaun.com
001,Surname1,Shaun,18/09/86,Sheffield,012345,Male,shaun@shaun.com
001,Surname1,Shaun,18/09/86,Sheffield,012345,Male,shaun@shaun.com

在我的文本文件(转换为 csv)中,我有以下数据(到目前为止):

001,Surname1,Shaun,18/09/86,Sheffield,012345,Male,shaun@shaun.com
002,Surname2,Ifty,01/01/01,Rotherham,0123456,Male,Ifty@ifty.com
003,Surname3,Dawn,01/01/01,Doncaster,0123456,Female,Dawn@Dawn.com
004,Surname4,Bryan,01/01/01,Sheffield,012345,Male,Bryan@deaf.com

因此,如果用户键入 'Sheffield',它应该显示 Shaun 和 Bryan 的信息。

如有任何帮助,我们将不胜感激。

如果数据库在 csv 文件中,我喜欢使用 pandas 这样的东西:

import pandas as pd
df = pd.read_csv('citydat.csv', header=None, 
     names=['code','sur','name','date','city','post','sex','email'])
df.set_index('code', inplace=True, drop=True)
cities = df.city.unique()
while True:
    city = input('enter city: ').title()
    if city in cities:
        print(df[df.city==city])
    elif city == '':
        break
    else:
        print(city,'not in database')

运行 看起来像这样:

enter city: new york
New York not in database
enter city: sheffield
          sur   name      date       city   post   sex            email
code                                                                    
1     Surname1  Shaun  18/09/86  Sheffield  12345  Male  shaun@shaun.com
4     Surname4  Bryan  01/01/01  Sheffield  12345  Male   Bryan@deaf.com
enter city: 

编辑:如果你没有 pandas,你可以使用 csv 模块来做这样的事情,它应该在你的 python 发行版中。

import csv
with open('citydat.csv') as csvfile:
    reader = list(csv.reader(csvfile))
while True:
    city = input('enter city: ').title()
    if city == '':
        break
    lines = [line for line in reader if line[4]==city] # city name is index 4
    if lines == []:
        print(city,'not in database')
    else:
        for line in lines:
            print(line)


enter city: new york
New York not in database
enter city: sheffield
['001', 'Surname1', 'Shaun', '18/09/86', 'Sheffield', '012345', 'Male', 'shaun@shaun.com']
['004', 'Surname4', 'Bryan', '01/01/01', 'Sheffield', '012345', 'Male', 'Bryan@deaf.com']
enter city: boston
Boston not in database
enter city:

如果您没有 csv 模块,或者出于某种原因不想使用它(?),那么您可以使用相同的逻辑,但将文件作为文本打开,并对行做一些进一步的处理.