大家好,我写了下面的代码,但是每当输入文本包含撇号('
)时,我就会收到一个错误
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)
出现以下错误:
% 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
详见http://python.org/dev/peps/pep-0263/ 任何想法,提前谢谢!你知道吗
您需要声明文件编码才能使用像“”这样的非ASCII字符。我认为拉丁语-1是python2的推荐编码,因此请将其放在文件的最开头(如果有shebang,请放在shebang之后):
# -*- coding: latin-1 -*-
选项1:
’
此字符未在ASCII中定义,请使用此字符来代替’
我替换了字符,没有错误:
选项2:
更改Python源代码编码:https://www.python.org/dev/peps/pep-0263/
示例:
相关问题 更多 >
编程相关推荐