0

I'm using Python 3.2.3 and idle to program a text game. I'm using a .txt file to store the map schemes that later will be opened by the program and draw at the terminal(IDLE for the moment).

What is in the .txt file is it:

╔════Π═╗
Π      ║ 
║w bb c□
║w bb c║ 
╚═□══□═╝

Π: door; □: window; b: bed; c: computer; w: wardrobe

As I'm new to programming I'm having a difficult problem doing this.

Here is the code I made so far for this:

doc =  codecs.open("D:\Escritório\Codes\maps.txt")
map = doc.read().decode('utf8')
whereIsmap = map.find('bedroom')
if buldIntel == 1 and localIntel == 1:
    whereIsmap = text.find('map1:')
    itsGlobal = 1
if espLocation == "localIntel" == 1:
    whereIsmap = text.find('map0:')
if buldIntel == 0 and localIntel == 0:
    doc.close()

for line in whereIsmap:
    (map) = line
    mapa.append(str(map))
doc.close()

if itsGlobal == 1:
    print(mapa[0])
    print(mapa[1])
    print(mapa[2])
    print(mapa[3])
    print(mapa[4])
    print(mapa[5])
    print(mapa[6])
    print(mapa[7])

if itsLocal == 1 and itsGlobal == 0:
    print(mapa[0])
    print(mapa[1])
    print(mapa[2])
    print(mapa[3])
    print(mapa[4])

There are two maps and each one of them has a title the smaller one is map1(the one I've show).

Python is giving this error message if I try to run the program:

Traceback (most recent call last):
  File "C:\Python32\projetoo", line 154, in <module>
    gamePlay(ask1, type, selfIntel1, localIntel, buildIntel, whereAmI, HP, time, itsLocal, itsBuild)
  File "C:\Python32\projetoo", line 72, in gamePlay
    map = doc.read().decode('utf8')
  File "C:\Python32\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

What do I do to print to the IDLE terminal the maps exactly as I showed up there?

schlamar
  • 9,238
  • 3
  • 38
  • 76
  • Are you sure your file is utf-8? Files starting with `\xff` most of the time are utf-16. – mata Jun 06 '12 at 18:27

1 Answers1

2

The issue is that you are using codecs.open without specifying an encoding, then trying to decode the string returned by doc.read(), even though it is already a Unicode string.

To fix this, specify an encoding in your call to codecs.open: codecs.open("...", encoding="utf-8"), then you won't need the call to .decode('utf-8') later.

Also, since you're using Python 3, you can just use open:

doc = open("...", encoding="utf-8").read()

Finally, you'll need to re-encode the unicode string when you print it:

print("\n".join(mapa[0:4]).encode("utf-8"))
David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • Got it. But what means "\n". I never used this code. What should I put there? – Victor Matheus Alves Ramos Jun 06 '12 at 18:41
  • print(u"\n".join(mapa[0:4]).encode("utf-8")) this code is outputting an error "invalid sintax" just after "\n at the (") signal. – Victor Matheus Alves Ramos Jun 06 '12 at 19:15
  • Oh, are you using Python3? If so, use `print("\n".join(mapa[0:4]).encode("utf-8"))`. I think. – David Wolever Jun 06 '12 at 19:25
  • And `"\n"` is the newline character. To see what it does: `print("foo\nbar")` – David Wolever Jun 06 '12 at 19:31
  • Got this error: Traceback (most recent call last): File "C:\Python32\projetoo", line 149, in gamePlay(ask1, type, selfIntel1, localIntel, buildIntel, whereAmI, HP, time, itsLocal, itsBuild) File "C:\Python32\projetoo", line 72, in gamePlay whereIsmap = map.find('bedroom') File "C:\Python32\lib\codecs.py", line 721, in __getattr__ return getattr(self.stream, name) AttributeError: '_io.BufferedReader' object has no attribute 'find' – Victor Matheus Alves Ramos Jun 06 '12 at 19:39
  • I think I'll just redraw the map in ASCII it wont be perfect but at least it's going to work. What do you think? – Victor Matheus Alves Ramos Jun 06 '12 at 19:45
  • Err, sorry, I accidentally a `.read()` in that example. Fixed now. – David Wolever Jun 06 '12 at 21:17
  • It's still giving a error: `UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte`. Also, sorry for the bottering. – Victor Matheus Alves Ramos Jun 06 '12 at 21:51
  • Ah, so in that case, you might need to follow @mata's suggestion and try using `utf-16` instead of `utf-8`. – David Wolever Jun 06 '12 at 22:51
  • It worked with `utf-16`, now I'm having an issue with this code `for line in searchMap: (mapa) = line mapa.append(str(mapa)) if itsGlobal == 1: print(mapa[0]) print(mapa[1]) print(mapa[2]) print(mapa[3]) print(mapa[4]) print(mapa[5]) print(mapa[6]) print(mapa[7])` It prints: `AttributeError: 'str' object has no attribute 'append'` – Victor Matheus Alves Ramos Jun 06 '12 at 23:45