python 编写代码接收文件路由和 return 包含文件中内容的元组的程序

python program to write a code that receives file route and return a tuple with things from the file

我需要编写一个程序来接收文件。 她的内容是这样的:

hello;Adele;5:21
easy on me;Adele;2:31
My Way;FrankSinatra;3:45

这是我的代码:

def my_mp3_plyalist(file_path):
f = open(file_path, "r")
# count lines
line_count = 0
for line in f:
    if line != "\n":
        line_count += 1
f.close()
# end count lines
with open(file_path, 'r') as f:
    result = tuple()
    splited_lines = f.read().split()
    len_splited_lines = len(splited_lines)
    items = list()
    for line in range(len_splited_lines):
        items.append(splited_lines[line].split(';'))
    print(items)

    max_song_len = 0
    longest_song_name = ""
    max_artist_apperance, most_appearing_artist = 0, ""
    artists = {}

    for i in items:
        song_len = float(i[-1].replace(':', '.'))
        if song_len > max_song_len:
            longest_song_name = i[0]
        for item in items:
            artist = item[0]
            if artist not in artists:
                artists[artist] = 0
            artists[artist] += 1
        most_appearing_artist, number_of_appearances = max(artists.items(), key=lambda x: x[1])

    result = (longest_song_name, line_count, most_appearing_artist)
    return result


def main():
   print(my_mp3_plyalist(r"C:\Users\user\Documents\words.txt"))


if __name__ == "__main__":
  main()

程序需要return一个包含(最长歌曲的名称、文件中的行数、文件中出现次数最多的艺术家)的元组

('hello', 3, 'Adele')

但它不起作用 return:

('MyWay', 3, 'hello')
from collections import Counter

with open("path/to/file") as f:
    lines = f.readlines()

lines = tuple(
    map(
        lambda x: (x[0], x[1], float(x[-1])),
        map(lambda x: x.strip().replace(":", ".").split(";"), lines),
    ),
)

longest = max(lines, key=lambda x: x[-1])[0]
artists_count = Counter([el[1] for el in lines])
max_artists_count = max(artists_count, key=artists_count.get)
obj = longest, len(lines), max_artists_count
print(obj)
# ('hello', 3, 'Adele')