包含撇号时Python在字符串中计数大写字母错误

包含撇号时Python在字符串中计数大写字母错误,python,string,Python,String,大家好,我编写了以下代码,但每当输入文本包含撇号(”)时,我就会收到一个错误 我收到以下错误: % python /Users/shirin/Desktop/capitalsnew.py [355] File "/Users/shirin/Desktop/capitalsnew.py", line 11 SyntaxError: Non-ASCII character '\xe2' in file /Users/shirin/Desktop/capitalsnew.py on li

大家好,我编写了以下代码,但每当输入文本包含撇号(
)时,我就会收到一个错误

我收到以下错误:

% python /Users/shirin/Desktop/capitalsnew.py      [355]
  File "/Users/shirin/Desktop/capitalsnew.py", line 11
SyntaxError: Non-ASCII character '\xe2' in file /Users/shirin/Desktop/capitalsnew.py on line 11, but no encoding declared
)

有关详细信息,请参阅
任何想法,提前谢谢

选项1:

% python /Users/shirin/Desktop/capitalsnew.py      [355]
  File "/Users/shirin/Desktop/capitalsnew.py", line 11
SyntaxError: Non-ASCII character '\xe2' in file /Users/shirin/Desktop/capitalsnew.py on line 11, but no encoding declared
此字符未在ASCII中定义,请使用此字符

from collections import Counter
import string


def count_letters(word):
    BAD_LETTERS = "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,  "
    return len([letter for letter in word if letter not in BAD_LETTERS])
    for letters in set(words):
            return count[letters]

word = "The Catcher in the Rye by J.D. Salinger TO MY MOTHER If you really want to hear about it, the first thing you’ll probably want to know is where I was born, an what my lousy childhood was like, and how my parents were occupied and all before they had me, and all that David Copperfield kind of crap, but I don’t feel like going into it, if you want to know the truth. In the first place, that stuff bores me, and in the second place, my parents would have about two hemorrhages apiece if I told anything pretty personal about them. They’re quite touchy about anything like that, especially my father. They’re nice and all, I’m not saying that, but they’re also touchy as hell. "
print count_letters(word)
我替换了字符,没有错误:

word = "The Catcher in the Rye by J.D. Salinger TO MY MOTHER If you really want to hear about it, the first thing you'll probably want to know is where I was born, an what my lousy childhood was like, and how my parents were occupied and all before they had me, and all that David Copperfield kind of crap, but I dont feel like going into it, if you want to know the truth. In the first place, that stuff bores me, and in the second place, my parents would have about two hemorrhages apiece if I told anything pretty personal about them. They're quite touchy about anything like that, especially my father. They're nice and all, I'm not saying that, but they're also touchy as hell. "
选项2:

% python /Users/shirin/Desktop/capitalsnew.py      [355]
  File "/Users/shirin/Desktop/capitalsnew.py", line 11
SyntaxError: Non-ASCII character '\xe2' in file /Users/shirin/Desktop/capitalsnew.py on line 11, but no encoding declared
更改Python源代码编码:

示例:

      #!/usr/bin/python
      # -*- coding: latin-1 -*-
      import os, sys
      ...

      #!/usr/bin/python
      # -*- coding: iso-8859-15 -*-
      import os, sys
      ...

      #!/usr/bin/python
      # -*- coding: ascii -*-
      import os, sys
      ...

您需要声明文件编码才能使用非ASCII字符(如“”)。我认为拉丁语-1是Python2的推荐编码,因此请将其放在文件的最开头(如果有,请放在shebang之后):


#-*-编码:拉丁语-1-*-

您在使用此代码时遇到了什么错误?抱歉,复制了错误的代码并进行了更正。您按照建议阅读了吗?您在for循环之前返回了一个值,因此for循环没有执行。我阅读了它,但我不明白这就是为什么我在这里提问。我是python新手,如果有人能告诉我如何在我的代码中修复它,我将不胜感激