9

I am using Spyder IDE , Python 3.5, which is a part of the anaconda distribution. Given below are the first few lines of the code:

# -*- coding: utf-8 -*-
"""
Created on Tue Sep 20 16:22:40 2016

@author: pavan
This program reads csv file from the given directory .
The input directory for this is : "C:\Users\pavan\Documents\Python Scripts\EOD from Yahoo"
The output file is "comprehensive_trend.xlsx"

"""
import pdb
import pandas as pd
from datetime import date, datetime, timedelta
import os, glob
# Delarations
full_path = os.path.realpath(__file__)
current_directory = os.path.dirname(full_path)
directory = current_directory + "\\EOD from Yahoo\\"
#directory = "C:\\Users\\pavan\Documents\\Python Scripts\\EOD from Yahoo\\"

I was running this code on Python 2.7 and it was working fine. Just recently I migrated to Python 3.5 and when I execute this code, I get the following output:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 145-146: truncated \UXXXXXXXX escape

After wrecking my head quite a bit, I deleted this line from the comments section:

The input directory for this is : "C:\Users\pavan\Documents\Python Scripts\EOD from Yahoo"

Now the program runs correctly.

My doubts:

  1. Why does this happen?
  2. What is the best way to write comments in Python 3.5 to avoid these kind of errors?
mzjn
  • 48,958
  • 13
  • 128
  • 248
Pavan
  • 209
  • 2
  • 10
  • 4
    `\U` (as in `C:\Users`) has a special meaning in strings in Python 3. Try adding an "r" directly in front of the comment to tell it that you don't want this behaviour, i.e. `r""" ... Created on ...`. – Phillip Sep 20 '16 at 11:38
  • 1
    Similar to http://stackoverflow.com/q/1347791/407651 – mzjn Sep 20 '16 at 11:42
  • 1
    Note that the "comments section" is a docstring. It is not a comment. See http://stackoverflow.com/q/33734170/407651. – mzjn Sep 20 '16 at 13:12

2 Answers2

15

I've recently had a similar problem for the first time using 'multi-line' commenting, so I did some research.

'multi-line' comment in python doesn't actually exist. As in, they're considered as strings (Hence why it can be used as docstrings). In fact they are treated as strings with no variable. This means the interpreter cannot ignore the 'multi-line' comments in your code and therefore any special characters need an escape \

Now knowing that they are treated as strings, there are two ways to keep your comments.

  1. Convert the comments into single lined comments. In many IDE, multi-line conversion commenting is possible. (Ctrl+K+C in VScode). This is recommended by PEP8

  2. slap r in front of your multi-lined comment block, to indicate the all the characters in the strings following will be taken in as raw

From your code

r"""
Created on Tue Sep 20 16:22:40 2016

@author: pavan
This program reads csv file from the given directory .
The input directory for this is : "C:\Users\pavan\Documents\Python Scripts\EOD from Yahoo"
The output file is "comprehensive_trend.xlsx"

"""
Hannest
  • 206
  • 3
  • 5
0

You are using \User in the comments and \U is interpreted as a unicode literal which failed to be decoded.

use \\User instead.

Similarly, \u should be replaced with \\u.

P.S. Python supports multi-line string literal as docstring and the usage here is perfectly fine and recommended.

Jingshao Chen
  • 3,405
  • 2
  • 26
  • 34