简体   繁体   中英

Getting a 'unicodeescape' codec error

I am a beginner using python 3.3 I think. I am trying to code a simple program that will ask the user what they want to search eg movie or by who's starring in the film (and all the film details will come up.)

I have written this code:

def displayuserinfo():

from collections import namedtuple

import csv

global file

file=namedtuple('file', 'movie, starring, comments, year')

global records

records=[""]*300

global addressbook

addressbook= open(r"C:\Users\Desktop\filmscsv.csv", "r")

global counter

counter=0

   for line in addressbook:

records[coounter] = file._make(line)

records = records + 1

def movie():

global movie

global moviecounter

found= False

movie= input("Enter the movie:")

movie= movie.lower()

moviecounter=0

def movie_validation():

moviecounter=0

for i in range(counter):

 if records[i].movie[:]== movie:

 print(records[i].movie)

moviecounter += 1


def starring():

 global starring

global starringcounter

found= False

starring= input("Enter who's starring:")

starring= starring.lower()

 starringcounter=0

def starring_validation():

starringcounter=0

 for i in range(counter):

if records[i].starring[:]== starring:

print(records[i].starring)

starringcounter += 1

#main
global choice

choice= str(input("What do you wish to do: 1. 2. 3.:"))

found= False

while found is not True:

if choice== "0":
        print("Goodbye!")

    elif choice == "1":
        displayuserinfo()
        movie()
        movie_validation()
        found= True

    elif choice== "2":
        displayuserinfo()
        starring()
        starring_validation()
        found = True
    else:
        print("Incorrect choice!")
        choice= input("What do you wish to do?:")

Every time I run the code this error appears: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\UXXXXXXXX escape

I have been trying to work out this program for a long time now. I have to do it with years as well but that's a different story. I am just trying to make it work and I know the file is correct but I just can't see where the problem is. Maybe because I don't have the mind of a programmer. Can anyone please suggest how to make this work? Appreciate any useful comments. PS Sorry about the code not being presented as code but I'm new to the website and haven't figured out how to do that yet.

It's because you've used \\ s, which is an escape character in Python. Try:

addressbook= open("C:/Users/Desktop/filmscsv.csv", "r")

instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM