2017-04-24 63 views
1

在下面的程序中,我創建了窗口輸出。我想將輸出重定向到文件。但我無法將window.inch(y,x)輸出寫入文件。無法在python中編寫window.inch(y,x)輸出到文件

#! /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) 

獲得以下錯誤,

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

@ inchar = window.getch()

請幫我在這方面。

+0

@ThomasDickey請看看 –

+0

錯誤@ output_file.write(inchar)不是i n inchar = window.getch() –

回答

0

問題是這裏:

  output_file.write(inchar) 

因爲inchar是一個數,含有8位字符和一些視頻屬性。你可以將其轉換成使用的東西串像

  output_file.write(str(inchar)) 

但帶來了另一個問題:在你的輸出文件確定在視頻屬性開始(和結束),並在你的角色實際上是。

+0

output_file.write(str(inchar)) - >它沒有工作。它只是打印由window.inch(y,x)返回的數字。我想要確切的窗口副本到文件中。例如 例如。如果我的窗口有「A h&」,那麼文件應該有「A h&」 –

0

你應該嘗試的window.getkey()方法,而不是window.getch()

如文檔中所述,getch()函數可以返回各種函數,光標和其他鍵的ASCII字符範圍之外的值。 getkey()將返回一個多字符字符串,標識那些不是簡單ASCII的字符。這在這裏解釋一下:Curses Programming with Python。如上所述,在命名模式KEY_xxx之後的模塊詛咒模塊中將非ASCII密鑰的名稱定義爲頂級符號(常數)。

試試看。

至於各地window.inch()你的循環......你需要每一通電話到(this_ascii_char = RET & 0xFF的,其中RET是整數返回屏蔽掉的返回值的低8位由window.inch())。

其餘位是該位置角色的curses屬性。 (目前還不清楚你想要做什麼,如果你想存儲屬性和ASCII字符,那麼看起來你需要爲每個ASCII字符編寫16位屬性。從屏幕的X * Y柵格中寫下所有的ASCII碼,然後打包所有相應屬性的二進制文件,然後當你讀取和處理數據時,你可以將文件的這兩部分放在一起,你的詛咒屏幕顯示設置循環。但我只是猜測。

真正的教訓是閱讀這些文檔。我提供的鏈接告訴你這些功能恢復。

+0

錯誤@ output_file.write(inchar)不在inchar = window.getch() –

+0

我添加了關於window.inch()vs 。window.getch()。總體問題是一樣的。這些函數返回超出ASCII範圍的整數值。 –

+0

Hi Jim, 我只是想打印出現在窗口中的所有輸出數據。 你的回答「this_ascii_char = ret&0xFF」解決了這個問題。但它沒有格式化。它以單行打印所有內容。我想編寫窗口內容以完全按照它在窗口中顯示的方式進行存檔。 我試圖寫this_ascii_char = ret&0xFFFF 16位。但它給下面的下面的代碼 ValueError異常錯誤:字符()精氨酸不在範圍內(256) VAL =(RET&0xFFFF的) output_file.write(CHR(VAL)) –