gpt4 book ai didi

python - 如何打印特定用户的 fav_genre 字段

转载 作者:行者123 更新时间:2023-12-01 02:26:56 25 4
gpt4 key购买 nike

因此,下面的代码将变量 username pass 等保存到文本文件中,并用冒号分隔每个字段。假设我想打印出特定用户最喜欢的类型,这怎么可能,因为到目前为止我的尝试都失败了。顺便说一句,这一切都在Python中。我编辑添加了我的尝试,但它不工作。有什么想法吗?

usrFile_write = open(textfile, 'a')
usrFile_write.write(username + ' : ' + password + ' : ' + name + ' : ' + dob + ' : ' + fav_artist + ' : ' + fav_genre + ' : ' + '\n')
print('New Account Created!')
print()
usrFile_write.close()
Menu()

我的尝试:

textfile = 'user_DB.txt'
username = 'dj'
def getUsersFavouriteGenre():
with open(textfile, 'r') as textIn:
for line in textIn:
information = line.split(' : ')
if information[0] == username:
return information[5]
return 'Failed to find user.'

getUsersFavouriteGenre()

最佳答案

你的代码看起来不错。我假设问题是您的程序没有输出任何内容,在这种情况下,我看到的唯一问题是将您的最后一行 getUsersFavouriteGenre() 更改为 print(getUsersFavouriteGenre())

如果您遇到特定错误或不需要的输出,您可以告知我们,我们可以为您提供更具体的帮助。

关于python - 如何打印特定用户的 fav_genre 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47296031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com