0

So I've been using Python for quite a long time and I've always used the following structure to print the variable:

dt = 5.5 print("dt = " + str(dt)) 

but I know there's a more elegant version of this command which is:

dt = 5.5 print("dt = %f" % dt) 

But I constantly get the error:

 Traceback (most recent call last): File "C:\Users\Komputer\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2980, in run_code self.showtraceback(running_compiled_code=True) File "C:\Users\Komputer\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 1849, in showtraceback self.showsyntaxerror(filename, running_compiled_code) File "C:\Users\Komputer\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 1911, in showsyntaxerror stb = self.SyntaxTB.structured_traceback(etype, value, elist) File "C:\Users\Komputer\Anaconda3\lib\site-packages\IPython\core\ultratb.py", line 1408, in structured_traceback newtext = linecache.getline(value.filename, value.lineno) File "C:\Users\Komputer\Anaconda3\lib\linecache.py", line 16, in getline lines = getlines(filename, module_globals) File "C:\Users\Komputer\Anaconda3\lib\linecache.py", line 47, in getlines return updatecache(filename, module_globals) File "C:\Users\Komputer\Anaconda3\lib\linecache.py", line 137, in updatecache lines = fp.readlines() File "C:\Users\Komputer\Anaconda3\lib\codecs.py", line 321, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf3 in position 83: invalid continuation byte 

What can be wrong? I suppose this is something with text codecs but I don't know where to change it. The same error shows up when printing other type of variables. I've looked up on several questions where this error appears but none of it was about printing.

I am using Spyder 3.2.8 (Python 3.6)

2
  • 1
    Your error message disagrees with the code you present. The message says that there is an undecodable character at positon 83 of the string being printed. But either of your two examples results in a string of length 3 (and both work as expected, identically). You need to show us the actual data that is causing the error message. Commented Mar 12, 2019 at 12:15
  • wait, i just looked this up. this is an error with the code iteself, you have a rogue character in there somewhere! Commented Mar 12, 2019 at 12:16

2 Answers 2

1

The error is not in the command you posted; your Python source file just contains non-UTF8 characters. Look for any special characters, and see if the text editor you wrote it with has an option for selecting the character encoding.

Edit: In the latin1 charset, the byte 0xf3 stands for ó, so maybe check if you're using that character anywhere...

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks a lot! You guys were right, I had similar but not the same code which contained the "ó" letter in printing.

What is interesting is that you can print "ó" by print("ó") but you can't do it by adding variables in the same "print" like print("ó" +str(dt)) or print("ó %f" %dt).

1 Comment

That's an interesting discovery! I would have expected the character to cause the same problem regardless where it occurred in the script.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.