How to Separate Data in a Dictionary from a .txt (Python)

1

I have a text file (attachment.txt), which I have to store in a dictionary, with Name as key and phone, value. the file has the following information:

--- nome telefone ---
Ailton-1197765445 Josefa-2178655434

I used the following code:

ref_files = open("anexo.txt", "r")
for linha in ref_files:
    valos = linha.split()
    print(valos[0],valos[:0],valos[1:])

ref_files.close()

but it returns values like this: Ailton-1197765445 [] ['Josefa-2178655434']

I created the dictionary but the return is the same:

dic = { k.split()[0]:k.split()[1:] for k in ref_files.readlines() }

Could anyone help me with this? Grateful.

    
asked by anonymous 12.11.2017 / 03:39

1 answer

0
def monta_dicionario(caminho=None):
    with open(caminho, "r") as anexo:
        dic = {}
        for linha in  anexo.readlines()[1:]:
            nomes = linha.split(" ")
            for nome_tel in nomes:
                val = nome_tel.split('-')
                dic[val[0]] = val[1]

    return dic

if __name__ == '__main__':
    dic = monta_dicionario('./anexo.txt')
    print(dic)
    
13.11.2017 / 03:26