【问题标题】:Display array values based on user input in Python在 Python 中根据用户输入显示数组值
【发布时间】:2015-12-09 10:05:06
【问题描述】:

我有一个文本文件来提供我的数组。我已经使用 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 的信息。

任何帮助将不胜感激。

【问题讨论】:

  • “但这不起作用” - 会发生什么?
  • 所以当我运行它并输入'Sheffield'时得到的输出是:001,Surname1,Shaun,18/09/86,Sheffield,012345,Male,shaun@shaun.com001,Surname1, Shaun,18/09/86,Sheffield,012345,男,shaun@shaun.com001,Surname1,Shaun,18/09/86,Sheffield,012345,男,shaun@shaun.com001,Surname1,Shaun,18/09/ 86,谢菲尔德,012345,男,shaun@shaun.com
  • 为什么同时使用inputraw_input?顺便说一句,使用.find 而不是index 并避免扫描两次
  • 也许if city_input in City: 应该是if city_input in row:
  • @Pynchia 执行上述操作会输出以下内容:001,Surname1,Shaun,18/09/86,Sheffield,012345,Male,shaun@shaun.com 001,Surname1,Shaun,18/09/ 86,谢菲尔德,012345,男,shaun@shaun.com

标签: python arrays csv input output


【解决方案1】:

如果数据库在 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 模块,或者由于某种原因不想使用它(?),那么您可以使用相同的逻辑,但将文件作为文本打开并在行上做一些进一步的处理。

【讨论】:

  • 谢谢,但我无法访问我正在使用的系统上的 pandas 库。有其他选择吗?
  • 太好了,感谢您的编辑。这很有魅力。谢谢@Steve Misuta
猜你喜欢
  • 2017-06-29
  • 1970-01-01
  • 2014-10-16
  • 1970-01-01
  • 2021-09-30
  • 2022-11-04
  • 1970-01-01
  • 2017-11-16
  • 2015-05-15
相关资源
最近更新 更多