python字典,读取文件行,添加到dict()

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python字典,读取文件行,添加到dict()相关的知识,希望对你有一定的参考价值。

这需要从文本文件.txt中读取,将文件读取后添加到字典中,以便用户以后可以搜索,添加/删除键和值。它确实要求输入文件名,但是在处理完文件后,它会崩溃并返回索引之外的contact [x [0] = y [1]。

加载文件

def fileload(filename):
    with open(filename,"r") as file:
        contact={}
        for line in file:
            x = line.replace(",","")
            y=x.split(':')
            contact[x[0]] = y[1]
            print('this is x value',x)

    return contact

向用户询问文件名,询问要搜索的用户,询问用户要添加/删除/更改(更新)的内容

  def main():
    print('To search through names and emails first select the file')
    filename=input('Whats the name of the .txt file?(Please add .txt) :  ')
    search=fileload(filename)

    try:
        search=fileload(filename)
    except:
        print("Sory file not found try again!")

    contact=fileload(filename)
    print('
----Would you like to search up an email by name?----')
    name_search=input('Whats the person called?: ')

尝试查找名称,尝试查找用户输入,然后根据每个用户请求修改用户输入

   for key,value in contact:
        if name_search == value:
            print(f'{name_search} is here!')

        else:
            print(f"{name_search} is not here!")
    print('Would you like to add/remove a name or email?')
    modify=input.lower("i would like to (remove or add)")
    try:
        if  modify  != remove or add:
            print("Sorry you can only choose remove or add!")
    except:
        if modify == remove:
            modify== remove
        if modify == add:
            modify=add

    modify_detail=input.lower(f"would you like to {modify} an email or name?: ")

    try:
        if modify_detail == name:
            modify_detail== name
        if modify_detail == email:
            modify_detail == email

    except:
        if modify  != email or name:
            print(f"please choose a name or email to {modify} ")
            modify_detail=input.lower(f"please pick if you want to {modify} name or email!: ")

    if modify == remove:
        if modify_detail== name:
            print("Pick a name that you want to remove")
            name_remove=input('what name do you want to remove?')
            del contact.get[name_remove]
        if modify_detail == email:
            print("Pick an email to remove or c")
            email_remove=input("what email will you be removing?")
            del contact.get[email_remove]
        print(contact)

    if modify == add:
        if modify_detail == name:
            print("Whats the name that you will be addind?")
            new_name=input("whats the new name?")
            contact["name"]=new_name
        if modify_detail == email:
            print("whats the new email?")
            new_email=input('new email:  ')
            contact["email"]=new_email
            print(contact)



main()
答案

唯一的原因可能是文件的至少一行中没有

以上是关于python字典,读取文件行,添加到dict()的主要内容,如果未能解决你的问题,请参考以下文章

python 字典dict和列表list的读取速度问题, range合并

python 读取csv文件并将其序列化为字典列表的脚本{column:value}然后可以将dicts用于计数或cr

[perl] perl 中二维哈希hash 与python 二维字典dict

python+selenium怎么读取csv中的数据进行列表循环登录自动化参数登录

python 保存和读取字典

python中读取mat文件