从数据表中提取Python

时间:2018-10-19 15:47:13

标签: python

我正在尝试创建代码以从数据表中提取。我的目标是找到最长的歌曲,一年中的歌曲以及一位艺术家的歌曲。问题是,当我运行当前拥有的内容时,我得到的返回值为0。显然,这是不正确的。我该怎么做才能解决这个问题?我在这里链接了数据表。点击here

def longest_song():         通过         def songs_by_year(year):         总数= 0         与open('music.csv','r')为f:             对于f中的行:                 时间= line.split(“,”)                 歌曲=时间[34]                 如果歌曲==年:                     总数=总数+ 1         总回报         def all_songs_by_artist(艺术家):         总计= int(0)

    data = open("music.csv", "r")
    for line in data:
        name = line.split(",")
        song = name[2]
        if song == artist:
            total = total + 1
        return total
    # --------------------------------------

    def menu():
    print()
    print("1. Identify longest song.")
    print("2. Identify number of songs in a given year.")
    print("3. Identify all songs by a given artist.")
    print("4. You choose something that is interesting and non-trivial.")
    print("5. Quit.")

    # --------------------------------------

    def main():
    choice = 0
    while (choice != 5):
        menu()
        choice = int(input("Enter your choice: "))
        if (choice == 1):
            longest_song()
        elif (choice == 2):
            year = int(input("Enter desired year: "))
            number = songs_by_year(year)
    ##            print("The number of songs from", "{:,d}".format(number))
            print(number)

        elif (choice == 3):
            artist = input("Enter name of artist: ").lower()
            all_songs_by_artist(artist)
            number = all_songs_by_artist(artist)
            print("There are", "{:,d}".format(number))
        elif (choice == 4):
            pass
        elif (choice != 5):
            print("That is not a valid option.  Please try again.")

    # --------------------------------------

    main()

1 个答案:

答案 0 :(得分:0)

您正在将输入艺术家转换为小写字母,但未将艺术家从文件转换为小写字母,因此没有匹配项。
您正在将Year转换为int,但未将其从文件转换为Year。
由于代码示例未正确缩进,因此很难判断是否还有其他问题。
我正在猜测,但我想它应该看起来像这样。

def longest_song(): pass

def songs_by_year(year):
    total=0
    with open('music.csv', 'r') as f:
        for line in f:
            time = line.split(",")
            song = time[34]
            try:
                if int(song) == year:
                    total = total + 1
            except:
                pass
        return total

def all_songs_by_artist(artist):
    total = int(0)
    with open("music.csv", "r") as data:
        for line in data:
            name = line.split(",")
            song = name[2].lower()
            if song == artist:
                total = total + 1
    return total
# --------------------------------------

def menu():
    print()
    print("1. Identify longest song.")
    print("2. Identify number of songs in a given year.")
    print("3. Identify all songs by a given artist.")
    print("4. You choose something that is interesting and non-trivial.")
    print("5. Quit.")

    # --------------------------------------

def main():
    choice = 0
    while (choice != 5):
        menu()
        choice = int(input("Enter your choice: "))
        if (choice == 1):
            longest_song()
        elif (choice == 2):
            year = int(input("Enter desired year: "))
            number = songs_by_year(year)
    ##            print("The number of songs from", "{:,d}".format(number))
            print(number)

        elif (choice == 3):
            artist = input("Enter name of artist: ").lower()
            all_songs_by_artist(artist)
            number = all_songs_by_artist(artist)
            print("There are", "{:,d}".format(number))
        elif (choice == 4):
            pass
        elif (choice != 5):
            print("That is not a valid option.  Please try again.")

# --------------------------------------

main()