【发布时间】:2017-01-28 07:19:03
【问题描述】:
我正在使用 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\\"
我在 Python 2.7 上运行此代码,它运行良好。就在最近我迁移到 Python 3.5 时,当我执行此代码时,我得到以下输出:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 145-146: truncated \UXXXXXXXX escape
经过一番折腾,我从 cmets 部分删除了这一行:
The input directory for this is : "C:\Users\pavan\Documents\Python Scripts\EOD from Yahoo"
现在程序可以正常运行了。
我的疑惑:
- 为什么会这样?
- 在 Python 3.5 中编写 cmets 以避免这些问题的最佳方法是什么 什么样的错误?
【问题讨论】:
-
\U(如C:\Users)在 Python 3 中的字符串中具有特殊含义。尝试在注释前直接添加一个“r”,告诉它你不想要这个行为,即r""" ... Created on .... -
请注意,“cmets 部分”是一个文档字符串。这不是评论。见stackoverflow.com/q/33734170/407651。
标签: python-2.7 unicode python-3.5