为了账号安全,请及时绑定邮箱和手机立即绑定

从文本文件中检索信息

从文本文件中检索信息

慕桂英546537 2024-01-16 10:37:55
我想接受用户输入的变量“name”,然后我想将account.zk(这是一个普通的文本文件)加载到一个列表中,然后我想检查输入是否已经在此列表中。如果没有,我想添加它,相反,我想跳过并继续。我自己编写了这个函数,但没有成功!有人能理解为什么吗?# Start Downloading Systemname = input(Fore.WHITE+"Enter Profile Name To Download: ")# Save Account (used for check the updates!)file_object = open("account.zk", "r")for line in file_object:  stripped_line = line.strip()  line_list = stripped_line.split()  list_of_lists.append(line_list)if (len(list_of_lists) != len(set(name))):    print("\n\nAlready in List!\n\n")    file_object.close    time.sleep(5)else:    file_object.close    file_object = open("account.zk", "w")    file_object.write(name + "\n")    file_object.close
查看完整描述

1 回答

?
猛跑小猪

TA贡献1858条经验 获得超8个赞

我认为你想做的是以下几点:


# Start Downloading System

name = input(Fore.WHITE + "Enter Profile Name To Download: ")


# Save Account (used for check the updates!)

file_object = open("account.zk", "r")


list_of_lists = []

for line in file_object:

  stripped_line = line.strip()

  line_list = stripped_line.split()

  list_of_lists.extend(line_list)  # add the elements of the line to the list


if name in list_of_lists:  # check if the name is already in the file

    print("\n\nAlready in List!\n\n")

    file_object.close()

    time.sleep(5)

else:  # if not, add it

    file_object.close()

    file_object = open("account.zk", "w")

    file_object.write(name + "\n")

    file_object.close()


查看完整回答
反对 回复 2024-01-16
  • 1 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信