Not able to write window.inch(y,x) output to file in python

蓝咒 提交于 2019-12-11 07:28:37

问题


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/python2.6
import sys, getopt
import curses
from curses import wrapper

def call(stdscr):
        inputFile = ''
        try:
            opts, args = getopt.getopt(sys.argv[1:],"hi:o:",["ifile=","ofile="])
        except getopt.GetoptError:
            print 'my_box.py -i <inputFile_With_Path>'
            sys.exit(2)
        for opt, arg in opts:
            if opt == '-h':
                print 'my_box.py -i <inputFile_With_Path>'
                sys.exit()
            elif opt == '-i':
                inputFile = arg

        output_file = open("/home/vparamasivam/Training/Python/output.txt", "w+")
        my_Dict = { }
        cur_y = 3
        cur_sbc = 30
        cur_EP1 = 10
        cur_uas = 50
        cur_UAC = 10
        sbc_cur = 24
        cur_poi = 2
        RED_TEXT = 1
        endPointCount = 0
        callCount = 0
        count = 0
        if not curses.has_colors():
            curses.endwin()
            print "no colors"
            sys.exit()
        else:
            curses.start_color()

        curses.init_pair(RED_TEXT, curses.COLOR_RED, curses.COLOR_BLACK)

        window = curses.newwin(500, 500, 0, 0)
        window.box()

        fo = open(inputFile, "r");

        print "Name of the file :", fo.name
        print "File closed or not :", fo.closed


        window.addstr(1, 29, "SBC", curses.color_pair(RED_TEXT))
        window.vline(cur_y, cur_sbc, '|', 500)
        for line in fo:
            if (line.find("Receive Msg From:") != -1) :
                ip, l, p, s, msg = line.split(',')
                ipc, ip = ip.rsplit(' ',1)
                ip, port = ip.split('/')
                ipPort = ip + ':' + port
                s, msg, req_uri = msg.split(' ', 2)
                print "\n SIP message : ", msg, ' '
                print "Received from IP : ", ipPort, ' '
                callCount += 1
                if not my_Dict.has_key(ipPort):
                    endPointCount +=1
                    if endPointCount == 1:
                        window.addstr(1, 4, ipPort, curses.color_pair(RED_TEXT))
                        window.vline(cur_y, cur_EP1, '|', 500)
                        new = {ipPort:cur_EP1}
                        my_Dict.update(new)
                        cur_EP1 += 7
                        cur_poi += 1
                        window.addstr(cur_poi, cur_EP1, msg, curses.color_pair(RED_TEXT))
                        cur_poi += 1
                        cur_EP1 -= 6
                        window.hline(cur_poi, cur_EP1, '-', 18)
                        cur_EP1 += 18
                        window.addstr(cur_poi, cur_EP1, '>')
                    else :
                        sbc_cur +=20
                        if endPointCount != 2:
                            cur_uas +=20
                        window.addstr(1, sbc_cur, ipPort, curses.color_pair(RED_TEXT))
                        window.vline(cur_y, cur_uas, '|', 500)
                        new = {ipPort:cur_uas}
                        my_Dict.update(new)
                        cur_poi +=2
                        cur_uas -=13
                        window.addstr(cur_poi, cur_uas, msg, curses.color_pair(RED_TEXT))
                        cur_uas +=13
                        cur_poi +=1
                        cur_sbc +=1
                        window.addstr(cur_poi, cur_sbc, '<')
                        count = cur_uas - cur_sbc - 1
                        cur_sbc +=1
                        window.hline(cur_poi, cur_sbc, '-', count)
                        cur_sbc -=2
                else :
                    ep = my_Dict[ipPort]
                    if ep == cur_UAC :
                        ep += 7
                        window.addstr(cur_poi, ep, msg, curses.color_pair(RED_TEXT))
                        cur_poi += 1
                        ep -= 6
                        window.hline(cur_poi, ep, '-', 18)
                        ep += 18
                        window.addstr(cur_poi, ep, '>')
                        cur_poi += 1
                    else :
                        cur_poi +=1
                        ep -=13
                        window.addstr(cur_poi, ep, msg, curses.color_pair(RED_TEXT))
                        ep +=13
                        cur_poi +=1
                        cur_sbc +=1
                        window.addstr(cur_poi, cur_sbc, '<')
                        count = ep - cur_sbc - 1
                        cur_sbc +=1
                        window.hline(cur_poi, cur_sbc, '-', count)
                        cur_sbc -=2

            elif (line.find("Send Msg to:") != -1) :
                ip, l, p, s, msg = line.split(',')
                s, msg, status = msg.split(' ', 2)
                ipc, ip = ip.rsplit(' ',1)
                ip, port = ip.split('/')
                ipPort = ip + ':' + port
                print "\n SIP message : ", msg, ' '
                print "Sent to IP : ", ipPort, ' '
                callCount += 1
                if not my_Dict.has_key(ipPort):
                    endPointCount +=1
                    if endPointCount == 1:
                        window.addstr(1, 9, ipPort, curses.color_pair(RED_TEXT))
                        window.vline(cur_y, cur_EP1, '|', 500)
                        new = {ipPort:cur_EP1}
                        my_Dict.update(new)
                        cur_poi +=1
                        cur_EP1 +=7
                        window.addstr(cur_poi, cur_EP1, msg, curses.color_pair(RED_TEXT))
                        cur_poi +=1
                        cur_EP1 -=6
                        window.addstr(cur_poi, cur_EP1, '<')
                        cur_EP1 +=1
                        window.hline(cur_poi, cur_EP1, '-', 18)
                    else :
                        sbc_cur +=20
                        if endPointCount != 2:
                            cur_uas +=20
                        window.addstr(1, sbc_cur, ipPort, curses.color_pair(RED_TEXT))
                        window.vline(cur_y, cur_uas, '|', 500)
                        new = {ipPort:cur_uas}
                        my_Dict.update(new)
                        cur_poi +=1
                        cur_uas -=13
                        window.addstr(cur_poi, cur_uas, msg, curses.color_pair(RED_TEXT))
                        cur_uas +=13
                        cur_poi +=1
                        cur_sbc +=1
                        count = cur_uas - cur_sbc - 1
                        window.hline(cur_poi, cur_sbc, '-', count)
                        cur_uas -=1
                        window.addstr(cur_poi, cur_uas, '>')
                        cur_uas +=1
                        cur_sbc -=1
                else :
                    ep = my_Dict[ipPort]
                    if ep == cur_UAC :
                        cur_poi +=1
                        ep +=7
                        window.addstr(cur_poi, ep, msg, curses.color_pair(RED_TEXT))
                        cur_poi += 1
                        ep -= 6
                        window.addstr(cur_poi, ep, '<')
                        ep +=1
                        window.hline(cur_poi, ep, '-', 18)
                        cur_poi += 1
                    else :
                        cur_poi +=1
                        ep -=13
                        window.addstr(cur_poi, ep, msg, curses.color_pair(RED_TEXT))
                        ep +=13
                        cur_poi +=1
                        cur_sbc +=1
                        count = ep - cur_sbc - 1
                        window.hline(cur_poi, cur_sbc, '-', count)
                        ep -=1
                        window.addstr(cur_poi, ep, '>')
                        cur_sbc -=1

        print ""
        for y in range(0,500):
            for x in range(0,500):
                inchar = window.inch(y,x)
                output_file.write(inchar)
        #curses.scr_dump(output_file)
        inchar = window.getch()
        output_file.close()
        fo.close()

def main(stdscr):

    call(stdscr)

wrapper(main)

Getting below error ,

TypeError: argument 1 must be string or read-only character buffer, not int

@ inchar = window.getch()

Please help me in this regard.


回答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.




回答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.



来源:https://stackoverflow.com/questions/43583901/not-able-to-write-window-inchy-x-output-to-file-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!