Code
Code

Reputation: 155

Python 2.7: decode error output not utf-8 on a simple string

When I try the following code:

a = "C:\Python27\777.xls"
print a

I get this error:

Decode error- output not utf-8

Although there are other questions which seem related, they mostly seem to be from people actually trying to encode something, whereas I am just trying to make sure my file path does not error out when I call it.

How can I fix it?

Upvotes: 1

Views: 2381

Answers (1)

Cody Piersall
Cody Piersall

Reputation: 8557

You need to escape your slashes: write \\ instead of just \ when you want a backslash.

a = "C:\\Python27\\777.xls"
print a

Alternatively, you could use a "raw string." Whenever a string is a raw string, a slash i just a slash, and you don't have to worry about weird stuff happening.

a = r"C:\Python27\777.xls"
print a

Right now, Python is interpreting the \777 as a single character, with the octal value 777, which does not exist. So Python is puking. Check out this link to the reference and scroll down a bit for the escape sequences in Python strings. https://docs.python.org/3/reference/lexical_analysis.html#strings

Upvotes: 4

Related Questions