Reputation: 297
I am running the following code in python and it's giving me this error:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
def filePro(filename):
f=open(filename,'r')
wordcount=0
for lines in f:
f1=lines.split()
wordcount=wordcount+len(f1)
f.close()
print ('word count:'), str(wordcount)
Please help me.
Upvotes: 2
Views: 3129
Reputation: 368894
Unicode literals (String literals in Python 3.x) with \U
or \u
escape sequence should be one of following forms:
>>> u'\U00000061' # 8 hexadecimals
'a'
>>> u'\u0061' # 4 hexadecimals
'a'
If there's not enough escape sequence, you get a SyntaxError.
>>> u'\u61'
File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-3: truncated \uXXXX escape
>>> u'\U000061'
File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-7: truncated \UXXXXXXXX escape
If you mean literal \
and U
. You'd better to use raw string:
>>> r'\u0061'
'\\u0061'
>>> print(r'\u0061')
\u0061
In the code you posted, there's no unicode escape sequence. You should Check other part of your code.
Upvotes: 3
Reputation: 79
Not sure, not much information provided here, but I guess python is trying to open the file with wrong encoding, you could open the file with the codecs library, use the correct codec to open the file, if I don't know or if it comes from windows I usually use 'cp1252' as this can open most types.
import codecs
def filePro(filename):
f = codecs.open(filename, 'r', 'cp1252'):
wordcount=0
for lines in f:
f1=lines.split()
wordcount=wordcount+len(f1)
f.close()
print ('word count:'), str(wordcount)
Another possibillity is that you have a filename that python translates to code, for example a file name like 'c:\Users\something' here the \U will be interpret. See this answer
Upvotes: 0