Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 例如,根据文本编辑器的字体,连字符几乎可以像连字符一样出现,而卷曲引号看起来很像直引号_Python - Fatal编程技术网

Python 例如,根据文本编辑器的字体,连字符几乎可以像连字符一样出现,而卷曲引号看起来很像直引号

Python 例如,根据文本编辑器的字体,连字符几乎可以像连字符一样出现,而卷曲引号看起来很像直引号,python,Python,这一行可以在Mac或Linux上运行,它将剥离不在ASCII可打印范围内的字符,并并排显示差异: # assumes Bash shell; for Bourne shell (sh), rearrange as a pipe and # give '-' as second argument to 'sdiff' instead sdiff --suppress-common-lines script.py <(tr -cd '\11\12\15\40-\176' <script.

这一行可以在Mac或Linux上运行,它将剥离不在ASCII可打印范围内的字符,并并排显示差异:

# assumes Bash shell; for Bourne shell (sh), rearrange as a pipe and
# give '-' as second argument to 'sdiff' instead
sdiff --suppress-common-lines script.py <(tr -cd '\11\12\15\40-\176' <script.py)

这也适用于macOS/BSD版本的grep。

我用pycharm修复了这个问题。在pycharm的底部可以看到文件编码。我注意到它是UT-8。我把它改成了US-ASCII

在文件顶部添加#编码:utf-8。您发布的内容没有问题;查看附近的行。添加了一个编辑类,看到这些行中的任何内容吗?你尝试过变种人的建议吗?您是否在文件中的任何位置都有“智能引号”(弯曲和/或有角度的类型)?一个可能导致它的示例是一个EN-DASH(
-
-
\xe2\x80\x93
)感谢这一帮助!仍然不确定宪章是什么。最后,我将代码扔进记事本,保存为ASCII,然后粘贴。我也遇到了这种情况,我认为这是由于复制粘贴问题,在编辑器(vim)中字符显示为空白。我也遇到了同样的问题,字符\xe2是连字符“–”(\xe2\x80\x93)的一部分,但比ASCII“-”稍长。这是因为我将文本粘贴到vim中,但没有注意这个较长的连字符。在整个故事中,我用一个双连字符“-”在一个维基文本(使用纺织)中生成了这个角色,我的是一个撇号-就像在寻找问题角色的人们的
O'Donnell
中一样。“UTF-8字节”列中以
%E2
开头的所有内容都是候选项。一般来说,问题是编辑代码时启用了“智能”功能,如“智能引号”将
替换为
(U+201C)和
(U+201D)或将
-
转换为
-
(U+2014 em破折号)。所有这些都以“\xe2\x80”开头“在UTF-8中,选项已更改为text->zap gremlins,在更高版本的TextWrangler中,但它对我有效:-)对我无效。下面总是显示错误。语法错误:第393行文件/home/aslam/projects/deva_26nov/mylibrary/email_constants.py中的非ASCII字符'\xe2',但未声明编码;请参阅以了解详细信息这不是选择答案的原因是什么?@cph我在问题提出4个月后写了它:)@cph因为,虽然这很有帮助,但选择的答案回答了“ANSI ASCII集中没有什么字符?”这两个答案都很好,在这种情况下,第一个答案通常会赢。这个答案对我来说很有效,我认为它是正确的。然而,从第一条评论到问题,答案都包含了解释。非常感谢你!在我的例子中,“'”字符0xe2在任何通用编码中都不是连字符。这是UTF-8编码的一部分,它编码了许多常用字符,比如非ASCII连字符和引号。虽然没有使用Django,但这仍然对我有帮助。我使用Python3编写脚本,并尝试使用Python2运行它。当我用正确的版本运行它时,错误消失了。谢谢我也有同样的错误,但在本地测试时,它并没有中断和工作。但当在服务器上运行时,它给出了那个编码错误。不得不将注释单引号替换为utf-8版本。这是一个糟糕的建议。你应该找出正确的编码;丢弃无法识别的字符只会损坏数据。这个问题问的是Python源代码中的编码,而不是输入文本文件中的编码。可能你是从某个博客或类似网站上复制/粘贴的,这些博客或类似网站的软件偷偷地将连字符和各种引用字符替换为“排版美观”但是不兼容的字形。如果你的来源不是拉丁-1,这将是非常错误的。您需要找出正确的编码,然后将其添加到
编码:
规范中。
def createLoadBalancer():
    conn = ELBConnection(creds.awsAccessKey, creds.awsSecretKey)
    hc = HealthCheck("instance_health", interval=15, target808="HTTP:8080/index.html")
    lb = conn.create_load_balancer('my_lb', ['us-east-1a', 'us-east-1b'],[(80, 8080, 'http'), (443, 8443, 'tcp')])
    lb.configure_health_check(hc)
    return lb
f = open('file','rt', errors='ignore')
with open("x.py") as fp:
    for i, line in enumerate(fp):
        if "\xe2" in line:
            print i, repr(line)
4 "\xe2        lb = conn.create_load_balancer('my_lb', ['us-east-1a', 'us-east-1b'],[(80, 8080, 'http'), (443, 8443, 'tcp')])\n"
# -*- coding: utf-8 -*-
import sys
print 'version is:', sys.version
# -*- coding: utf-8 -*-
Python will default to ASCII as standard encoding if no other
encoding hints are given.

To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as:

      # coding=<encoding name>

or (using formats recognized by popular editors)

      #!/usr/bin/python
      # -*- coding: <encoding name> -*-

or

      #!/usr/bin/python
      # vim: set fileencoding=<encoding name> :
def convert_freaking_apostrophe(self,string):

   try:
      issuer_rename = string.decode('windows-1252')
   except:
      issuer_rename = string.decode('latin-1')
   issuer_rename = issuer_rename.replace(u'’', u"'")
   issuer_rename = issuer_rename.encode('ascii','ignore')
   try:
      os.rename(directory+"/"+issuer,directory+"/"+issuer_rename)
      print "Successfully renamed "+issuer+" to "+issuer_rename
      return issuer_rename
   except:
      pass

#HANDLING FOR FUNKY APOSTRAPHE
if re.search(r"([\x90-\xff])", issuer):
   issuer = self.convert_freaking_apostrophe(issuer)
SyntaxError: Non-ASCII character '\xe2' in file
# coding: utf-8
In [1]: array = [[24.9, 50.5]​, [11.2, 51.0]]        # Raises an error

In [2]: string = "[[24.9, 50.5]​, [11.2, 51.0]]"     # Manually paste the above array here

In [3]: string
Out [3]: '[[24.9, 50.5]\xe2\x80\x8b, [11.2, 51.0]]' # Here they are!
#!/usr/local/bin/python
# coding: latin-1
#-*- mode: python -*-
# -*- coding: utf-8 -*-
# assumes Bash shell; for Bourne shell (sh), rearrange as a pipe and
# give '-' as second argument to 'sdiff' instead
sdiff --suppress-common-lines script.py <(tr -cd '\11\12\15\40-\176' <script.py)