[英]Getting a 'unicodeescape' codec error
I am a beginner using python 3.3 I think. 我认为我是使用python 3.3的初学者。 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 每次我运行代码时,都会出现此错误:SyntaxError:(unicode错误)'unicodeescape'编解码器无法解码位置2-3中的字节:截断\\ UXXXXXXXX转义
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.
PS抱歉,代码没有作为代码显示,但是我是该网站的新手,还没有弄清楚该怎么做。
It's because you've used \\
s, which is an escape character in Python. 这是因为您使用了
\\
s,它是Python中的转义字符。 Try: 尝试:
addressbook= open("C:/Users/Desktop/filmscsv.csv", "r")
instead. 代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.