dictionary - Python ValueError: chr() arg not in range(256) -
so learning python , redoing old projects. project involves taking in dictionary , message translated command line, , translating message. (for example: "btw, hello how r u" translated "by way, hello how you".
we using scanner supplied professor read in tokens , strings. if necessary can post here too. heres 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)
heres main translate.py file:
from support import * scanner import * import sys def main(): arguments = len(sys.argv) if arguments != 3: print'need 2 arguments!\n' exit(1) commanddict = sys.argv[1] commandmessage = sys.argv[2] dictionary,count = makedictionary(commanddict) message,messagecount = makemessage(commandmessage) print(dictionary) print(message) = 0 while count < messagecount: translation = translate(message[i],dictionary,messagecount) print(translation) count = count + 1 = +1 main()
and here support.py file 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): = 0 while != count: if word == dictionary[i]: return dictionary[i+1] = i+1 else: return word = 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
does know whats going on here? i've looked through several times , dont know why readstring throwing error... stupid missed
chr(0x2018)
work if use python 3.
you have code that's written python 3 run python 2. in python 2 chr
give 1 character string in ascii range. 8-bit string, maximum parameter value chr
is 255
. in python 3 you'll unicode character , unicode code points can go higher values.
Comments
Post a Comment