Python ValueError:chr()arg不在范围内(256) [英] Python ValueError: chr() arg not in range(256)
问题描述
所以我正在学习python并重做一些旧项目。这个项目涉及从字典中提取字典和消息,然后翻译消息。 (例如: btw,您好,您好吗?将翻译为顺便问一下,您好吗?。
So I am learning python and redoing some old projects. This project involves taking in a dictionary and a message to be translated from the command line, and translating the message. (For example: "btw, hello how r u" would be translated to "by the way, hello how are you".
我们使用的是教授提供的扫描仪读取令牌和字符串。如有必要,我也可以将其发布在这里。这是我的错误:
We are using a scanner supplied by the professor to read in tokens and strings. If necessary I can post it here too. Heres my error:
Nathans-Air-4:py1 Nathan$ python translate.py test.xlt test.msg
Traceback (most recent call last):
File "translate.py", line 26, in <module>
main()
File "translate.py", line 13, in main
dictionary,count = makeDictionary(commandDict)
File "/Users/Nathan/cs150/extra/py1/support.py", line 12, in makeDictionary
string = s.readstring()
File "/Users/Nathan/cs150/extra/py1/scanner.py", line 105, in readstring
return self._getString()
File "/Users/Nathan/cs150/extra/py1/scanner.py", line 251, in _getString
if (delimiter == chr(0x2018)):
ValueError: chr() arg not in range(256)
这里是我的主要translate.py文件:
Heres my main translate.py file:
from support import *
from scanner import *
import sys
def main():
arguments = len(sys.argv)
if arguments != 3:
print'Need two arguments!\n'
exit(1)
commandDict = sys.argv[1]
commandMessage = sys.argv[2]
dictionary,count = makeDictionary(commandDict)
message,messageCount = makeMessage(commandMessage)
print(dictionary)
print(message)
i = 0
while count < messageCount:
translation = translate(message[i],dictionary,messageCount)
print(translation)
count = count + 1
i = i +1
main()
这是我正在使用的support.py文件...
And here is my support.py file I am using...
from scanner import *
def makeDictionary(filename):
fp = open(filename,"r")
s = Scanner(filename)
lyst = []
token = s.readtoken()
count = 0
while (token != ""):
lyst.append(token)
string = s.readstring()
count = count+1
lyst.append(string)
token = s.readtoken()
return lyst,count
def translate(word,dictionary,count):
i = 0
while i != count:
if word == dictionary[i]:
return dictionary[i+1]
i = i+1
else:
return word
i = i+1
return 0
def makeMessage(filename):
fp = open(filename,"r")
s = Scanner(filename)
lyst2 = []
string = s.readtoken()
count = 0
while (string != ""):
lyst2.append(string)
string = s.readtoken()
count = count + 1
return lyst2,count
有人知道这是怎么回事吗?我已经浏览了好几次,但我不知道为什么readString会引发此错误...我可能错过了一些愚蠢的事情
Does anyone know whats going on here? I've looked through several times and i dont know why readString is throwing this error... Its probably something stupid i missed
推荐答案
chr(0x2018)
将在您使用Python 3的情况下工作。
chr(0x2018)
will work if you use Python 3.
您有为Python 3编写的代码,但您可以使用Python 2运行它。在Python 2中, chr
将为您提供一个ASCII范围内的字符串。这是一个8位字符串,因此 chr
的最大参数值为 255
。在Python 3中,您将获得一个unicode字符,并且unicode代码点可以提高到更高的值。
You have code that's written for Python 3 but you run it with Python 2. In Python 2 chr
will give you a one character string in the ASCII range. This is an 8-bit string, so the maximum parameter value for chr
is 255
. In Python 3 you'll get a unicode character and unicode code points can go up to much higher values.
这篇关于Python ValueError:chr()arg不在范围内(256)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!