我使用的是 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
在绞尽脑汁之后,我从评论部分删除了这一行:
The input directory for this is : "C:\Users\pavan\Documents\Python Scripts\EOD from Yahoo"
现在程序正确运行了。
我的疑问:
- 为什么会出现这种情况?
- 在 Python 3.5 中编写注释以避免这些问题的最佳方法是什么
什么样的错误?
我最近第一次使用“多行”注释时遇到了类似的问题,所以我做了一些研究。
python 中的“多行”注释实际上并不存在。 https://www.codecademy.com/en/forum_questions/505ba3cfc6addb000200e33c例如,它们被视为字符串(因此它可以用作文档字符串)。事实上,它们被视为没有变量的字符串。这意味着解释器无法忽略代码中的“多行”注释,因此任何特殊字符都需要转义\
现在知道它们被视为字符串,有两种方法可以保留您的评论。
将注释转换为单行注释。在许多 IDE 中,多行转换注释是可能的。 (Ctrl+K+C
在 VScode 中)。这是 PEP8 推荐的 https://www.python.org/dev/peps/pep-0008/#block-comments
slap 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"
"""
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)