如何在python3中按名称按字母顺序组织csv文件?

时间:2018-12-06 02:05:51

标签: python python-3.x

我的代码如下。我的代码基本上是某种宠物发现者。我有一个主菜单,该菜单允许用户单击特定数字时执行特定任务。现在有4个选择。

如果要按字母顺序组织该csv文件中的内容,我想添加第5个。这将称为def sortList()。

import csv

import pets
pets1=[] # stores each type of pets (name, birthdate, breed, color)
kinds=[] # stores type of pets as string (Dog, Fish, Bird, ...)


def load_file():
    # opens file to read

    file_reader= open('pets.csv')
    read = csv.reader(file_reader)

    #row-wise iteration
    for row in read: # each row is read, i.e row=[Fish, Nemo, April 2nd, GoldFish, Orange,',']

        r=row[:-1] # removes ',' from row
        kind=r[0].strip() # removes extra whitespaces from string
        name=r[1].strip() # name of pet
        birthdate=r[2].strip()
        breed=r[3].strip()
        color=r[4].strip()

        # based of type/breed, different objects are created
        if(kind=='Rabbit'):
            rabbit=pets.Rabbit(name, birthdate, breed, color)
            pets1.append(rabbit)
            kinds.append('Rabbit')

        elif(kind=='Cat'):
            cat=pets.Cat(name, birthdate, breed, color)
            pets1.append(cat)
            kinds.append('Cat')

        elif(kind=='Dog'):
            dog=pets.Dog(name, birthdate, breed, color)
            pets1.append(dog)
            kinds.append('Dog')

        elif(kind=='Bird'):
            bird=pets.Bird(name, birthdate, breed, color)
            pets1.append(bird)
            kinds.append('Bird')


#1, prints ot names of all pets
def display_name():
    for p in pets1:
        print(p.name)


#2 user_input=pet)name is searched in pets1
#if found, then printed out
def search_pet():
    pet_name=str(input('Enter name of a pet: '))
    index=0
    found=0
    print('Searching '+pet_name+': ')
    for p in pets1:
        if(pet_name==p.name):
            found=1
            print('Found at:', index, p.name, p.birthdate, p.breed, p.color)
            break
        index+=1
    if(found is 0):
        print(pet_name+' not found in the list. ')


#3 list of pets with details: name, birthdate, breed, color
def display_list():
    print(' List of pets: ')
    for p in pets1:
        print(p.name, p.birthdate, p.breed, p.color)



#4 pets of particular type are printed out
def pet_type_search():
    pet_type=str(input('Enter pet type: '))
    for i in range(len(kinds)):
        if(kinds[i]==pet_type):
            p=pets1[i]
    print(p.name, p.birthdate, p.breed, p.color)

def showMenu():
    print('1. Show pet names')
    print('2. Search for pet')
    print('3. Show list')
    print('4. Show pets of certain type')
    print('5. Exit')


# main method 
def main():
    # first load the file
    load_file()
    showMenu()
    choice = int(input('Enter choice: '))

    while choice != 5:
        if choice == 1:
            display_name()
        elif choice == 2:
            search_pet()
        elif choice == 3:
            display_list()
        elif choice == 4:
            pet_type_search()

        showMenu()
        choice = int(input('Enter choice: '))

# call main method
main()

0 个答案:

没有答案