Python 3.5 中的注释给出 unicode 错误
Comments in Python 3.5 giving unicode error
我正在使用 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 中写注释的最佳方式是什么来避免这些
错误类型?
我最近第一次使用 'multi-line' 评论遇到了类似的问题,所以我做了一些研究。
'multi-line' comment in python doesn't actually exist. 如前所述,它们被视为字符串(因此它可以用作文档字符串)。事实上,它们被视为没有变量的字符串。这意味着解释器不能忽略代码中的 'multi-line' 注释,因此任何特殊字符都需要转义 \
现在知道它们被视为字符串,有两种方法可以保留您的评论。
将注释转换为单行注释。在许多 IDE 中,多行转换注释是可能的。 (VScode 中的Ctrl+K+C
)。 This is recommended by PEP8
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"
"""
您在评论中使用了 \User
,\U
被解释为无法解码的 unicode 文字。
改用\User
。
同样,\u
应该替换为\u
。
P.S。 Python 支持多行字符串字面量作为 docstring,这里的用法非常好,推荐使用。
我正在使用 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 中写注释的最佳方式是什么来避免这些 错误类型?
我最近第一次使用 'multi-line' 评论遇到了类似的问题,所以我做了一些研究。
'multi-line' comment in python doesn't actually exist. 如前所述,它们被视为字符串(因此它可以用作文档字符串)。事实上,它们被视为没有变量的字符串。这意味着解释器不能忽略代码中的 'multi-line' 注释,因此任何特殊字符都需要转义 \
现在知道它们被视为字符串,有两种方法可以保留您的评论。
将注释转换为单行注释。在许多 IDE 中,多行转换注释是可能的。 (VScode 中的
Ctrl+K+C
)。 This is recommended by PEP8slap
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"
"""
您在评论中使用了 \User
,\U
被解释为无法解码的 unicode 文字。
改用\User
。
同样,\u
应该替换为\u
。
P.S。 Python 支持多行字符串字面量作为 docstring,这里的用法非常好,推荐使用。