Skip to content
Advertisement

“Python: ‘unicodeescape’ codec can’t decode bytes…: malformed N character escape” when I’m trying to type an input statement

def option3():
    copy("EMPLOYEE.DAT", "EMPLOYEE.DAT.bak")
    oldfile = open("EMPLOYEE.DAT.bak")
    newfile = open("EMPLOYEE.DAT", "w")
    data = oldfile.readlines()
    empdict = {}
    for line in data:
        ID, Name, Number, empType = line.split('|')
        empType = empType.rstrip('n')
        empdict[ID] = [Name, Number, empType]
    requested_ID = input("Enter the ID you want to modify: ")
    for each_ID in empdict:
        if each_ID == requested_ID:
            print("Requested Employee Data:n{}n{}n{}n{}".format(each_ID, empdict[each_ID][0], empdict[each_ID][1], empdict[each_ID][2]))
            correct_data = input("Is this the correct employee data [YN]: ")
            if correct_data == 'Y':
                pass

I was creating a program to store employee-related data. as a mini project. However as I tried to run my (barely-completed program_ I get the unicode error message and Python highlights the parenthesis after input. I’ve googled for answers but it seems like the common issue is related to file paths which I am not dealing with here.

This is just a section of the entire code and if needed I will edit this post and post it here.

Also when I tried to run it by double clicking the file the command prompt just shuts down immediately, but if I remove correct_data = input("Is this the correct employee data [YN]: ") then it works normally.

Advertisement

Answer

Either replace with /, i.e.:

correct_data = input("Is this the correct employee data [Y/N]: ")

or escape by replacing it with \, i.e.

correct_data = input("Is this the correct employee data [Y\N]: ")

or use a raw string:

correct_data = input(r"Is this the correct employee data [YN]: ")

For more details see string literals from the documentation.

Advertisement