pythonsyntax-errorstring-literals

Comments in Python 3.5 giving unicode error


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?

Solution

  • 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"
    
    """