有人可以告诉我如何将.text文件放入包含有数字和python字符串的字典中

问题描述 投票:0回答:1

我正在制作有关联系人保护程序的项目,但是我无法读取代码中的单个联系人...请帮我...我正在提供代码和问题如果您可以为我提供寻找单个用户或所有相关用户的整个过程...谢谢...

what = input("Do you want to read a single contact(y, n): ")
if what == 'y':
    who = input("Please Enter the name: ")
    a = open('contacts.txt', 'a')
    for line in a:
        k, v = line.strip().split('=')
        users[k.strip()] = v.strip()
        a.close()

    for i in users:
        if who.lower() == i.lower() or i.startswith(who[:3]):
            print(i)

这是错误:'

追踪(最近通话):文件“ C:/ Users / Teerth Jain / Desktop / teerth_made_projects / contacts.py”,第18行,在对于以下行:io.UnsupportedOperation:不可读,请在此处输入代码

'

python python-3.x dictionary text-files
1个回答
0
投票

[您已经以写入模式而不是读取模式打开文件,请使用"r"而不是"a"(附加在文件后)

a = open('contacts.txt', 'r')
© www.soinside.com 2019 - 2024. All rights reserved.