B
B
Bogdan2021-11-06 15:43:56
Python
Bogdan, 2021-11-06 15:43:56

Error TypeError: sequence item 0: expected str instance, dict found what to do?

import json
persones = []

def users_persone():


    name = input('Введите имя контакта - ')
    tel = input('Введите номер - ')

    persone = {name: tel}

    persones.append(persone)
    MyFile = open('C:\HP\proverka.txt', 'w')
    MyFile.writelines(persones)
    MyFile.close()

    with open('persones.json', 'w') as file:
        json.dump(persones, file, ensure_ascii=False)
    return persone


def main():
    l = True
    while l:

        users_choise = input("Желаете ввести или получить контактные данные - ")

        if users_choise == 'ввести':
            users_persone()



        elif users_choise == "получить":
            print(MyFile)

        users_choise_two = input("Желаете остановить програму - ")

        if users_choise_two == 'да':
            print("Програма закрыта")
            l = False


main()


I want the code to save the number and name to a text file, but it gives an error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-11-06
@bolossev666

MyFile = open('C:\HP\proverka.txt', 'w')
MyFile.writelines(persones)
MyFile.close()
writelines() expects an array of STRINGS as input, and you give an array of dictionaries. Hence the error.
Why did you write this fragment at all, if you have normal saving in json a little further?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question