Skip to content Skip to sidebar Skip to footer

Not Able To Write Window.inch(y,x) Output To File In Python

In the below program, after i created the window output. I want to redirect the output to the file. But i am not able to write the window.inch(y,x) output to the file. #! /usr/bin

Solution 1:

The problem is here:

            output_file.write(inchar)

because inchar is a number, containing an 8-bit character and some video attributes. You could convert that to a string using something like

            output_file.write(str(inchar))

but that introduces another problem: determining in your output file where the video attributes start (and end) and where your character actually is.

Solution 2:

You should probably try the window.getkey() method rather than window.getch().

As noted in the documentation the getch() function can return a value outside the range of ASCII characters for various function, cursor and other keys. getkey() will return a multi-character string identifying those characters which aren't simple ASCII. This is explained a bit here: Curses Programming with Python. As noted the names of the non-ASCII keys are defined as top level symbols (constants) in the curses module all following the naming pattern KEY_xxx.

Try that.

As for your loop around window.inch() ... you need to mask off the lower 8 bits of the return value from each call to that (this_ascii_char = ret & 0xFF, where ret was the integer returned by window.inch()).

The remaining bits are the curses attributes of the character at that location. (It's not clear what you want to do with those. If you want to store the attributes as well as the ASCII character than it looks like you'd need to write 16-bits of attributes for every ASCII character. It might make sense to write all the ASCII from your X*Y raster of the screen, following by the packed binary of all the corresponding attributes. Then when you read and processes the data you can zip() these two sections of the file together and feed them to your curses screen display loop. But I'm just speculating.

The real lesson here is read those docs. The links I've provided tell you what these functions are returning.

Post a Comment for "Not Able To Write Window.inch(y,x) Output To File In Python"