Create a list from a txt file with all italian words, python -
i downloaded here ( https://sourceforge.net/project/showfiles.php?group_id=128318&package_id=141110 ) dictionary italian words. wanted add them list use randint(0, list.count()) use random italian word string. little program "guess word". used guide: creating list of every word text file without spaces, punctuation see achieve this, received ascii code errors. tried replace odd characters (‡ËÈÏÚ) effective characters (àèèìù) , removed first line of thesaurus.txt file, still same ascii error. thank in advance help, i'm newbie in python. oh, , here's code, can recreate ascii error (you can download .txt file link above).
import re random import * file = open('thesaurus.txt', 'r') # .lower() returns version upper case characters replaced lower case characters. text = file.read().lower() file.close() # replaces not lowercase letter, space, or apostrophe space: text = re.sub('[^a-z\ \']+', " ", text) words = list(text.split()) word = random.choice(words) wordlist = list(word) guess = [] prove = [] l in range(len(word)): guess.append("_") attempts_remaining = 6 print (guess) print("you have", attempts_remaining, "attempts") guessed = 0 while guessed < len(word) , attempts_remaining != 0: if attempts_remaining != 0: guessed_this_time = 0 prova = input("write letter: ") prove.append(prova) if prove.count(prova) > 1: prova = input("you've tried letter! try another: ") l in range(len(word)): if wordlist[l] == prova: guess[l] = wordlist [l] guessed += 1 guessed_this_time += 1 else: pass if guessed_this_time == 0: attempts_remaining -= 1 print(attempts_remaining, "attempts remaining") elif guessed == len(word): print("you guessed it!") print(guess) if attempts_remaining == 0: print("you lost, word was", word)
the error is:
traceback (most recent call last): file "/users/user/documents/python/impiccato/impiccato.py", line 5, in <module> text = file.read().lower() file "/library/frameworks/python.framework/versions/3.4/lib/python3.4/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] unicodedecodeerror: 'ascii' codec can't decode byte 0x88 in position 2170: ordinal not in range(128)
edit: "solved" moving txt content .py , calling way:
text = """ here's list (the whole thing 19414ln lol) ... """ words = \ re.sub('[^a-z\ \']+', " ", text).split() # stores secret words in list
this way commas replaced spaces.
it encoding. don't know website had set as, not want. open in favorite editor , set encoding utf-8
. still won't work, because of bug: .count()
not give amount of items. want len(words)
.
Comments
Post a Comment