Pav*_*van 5 unicode python-2.7 python-3.5
我正在使用 Spyder IDE,Python 3.5,它是 anaconda 发行版的一部分。下面给出了代码的前几行:
# -*- 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\\"
Run Code Online (Sandbox Code Playgroud)
我在 Python 2.7 上运行此代码并且运行良好。就在最近,我迁移到 Python 3.5,当我执行此代码时,我得到以下输出:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 145-146: truncated \UXXXXXXXX escape
Run Code Online (Sandbox Code Playgroud)
在我的脑子里一闪而过之后,我从评论部分删除了这一行:
The input directory for this is : "C:\Users\pavan\Documents\Python Scripts\EOD from Yahoo"
Run Code Online (Sandbox Code Playgroud)
现在程序可以正常运行了。
我的疑惑:
小智 8
我最近第一次使用“多行”评论遇到了类似的问题,所以我做了一些研究。
python 中的“多行”注释实际上并不存在。就像在,它们被视为字符串(因此它可以用作文档字符串)。事实上,它们被视为没有变量的字符串。这意味着解释器不能忽略代码中的“多行”注释,因此任何特殊字符都需要转义\
现在知道它们被视为字符串,有两种方法可以保留您的注释。
将注释转换为单行注释。在许多 IDE 中,多行转换注释是可能的。(Ctrl+K+C
在 VScode 中)。这是 PEP8 推荐的
r
在你的多行注释块前面打一巴掌,表示后面字符串中的所有字符都将被视为原始字符
从你的代码
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"
"""
Run Code Online (Sandbox Code Playgroud)