Python 3.5 中的注释给出了 unicode 错误 [英] Comments in Python 3.5 giving unicode error

查看:78
本文介绍了Python 3.5 中的注释给出了 unicode 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Spyder IDE,Python 3.5,它是 anaconda 发行版的一部分.以下是代码的前几行:

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

我在 Python 2.7 上运行此代码并且运行良好.就在最近,我迁移到 Python 3.5,当我执行此代码时,我得到以下输出:

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.

我的疑惑:

  1. 为什么会发生这种情况?
  2. 在 Python 3.5 中编写注释以避免这些的最佳方法是什么有什么错误?

推荐答案

我最近第一次在使用多行"注释时遇到了类似的问题,所以我做了一些研究.

I've recently had a similar problem for the first time using 'multi-line' commenting, so I did some research.

python 中的多行"注释实际上并不存在.,它们被视为字符串(因此它可以用作文档字符串).事实上,它们被视为没有变量的字符串.这意味着解释器不能忽略代码中的多行"注释,因此任何特殊字符都需要转义 \

'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. 将注释转换为单行注释.在许多 IDE 中,多行转换注释是可能的.(Ctrl+K+C 在 VScode 中).这是 PEP8 推荐的

  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

在你的多行注释块前面打r,表示后面字符串中的所有字符都将被视为原始字符

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

来自您的代码

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"

"""

这篇关于Python 3.5 中的注释给出了 unicode 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆