【问题标题】:Plotext and Curses - windowsPlotext 和诅咒 - 窗口
【发布时间】:2022-08-14 20:33:04
【问题描述】:

需要此代码的帮助:

import plotext as plt
from contextlib import redirect_stdout
import io
import curses
import locale
locale.setlocale(locale.LC_ALL, \'\')
code = locale.getpreferredencoding()

PlotFile = io.StringIO()

with redirect_stdout(PlotFile):
    y = plt.sin() # sinusoidal signal 
    plt.scatter(y, marker=\'dot\', )
    plt.title(\"Scatter Plot\")
    plt.plot_size(10, 10)
    plt.show()
PlotFile.seek(0)
w = PlotFile.readlines()
allchar = []
vischar = []
dechar = []
bychar = []
def main (stdscr):
    curses.start_color()
    curses.use_default_colors()
    k = 0
    for i in w:
        for c in i:
            if ord(c) not in allchar:
                allchar.append(ord(c))
                vischar.append(c)
                dechar.append(c.encode(\'utf-8\'))
                bychar.append(bytes(dechar[-1]))
            stdscr.addstr(chr(ord(c)).encode(\'utf-8\'))
        print(i)
        k += 1
   stdscr.refresh()
   curses.napms(3000)

curses.wrapper(main)


print(allchar)
print(vischar)
print(dechar)
print(bychar)

在 Windows python 3.9 和 wincurses 上使用 VScode。虽然在普通终端 plotext 上工作正常,但在显示奇怪字符的诅咒中,如 [[m , [[107m 等。尝试了编码和uft mess,仍然无法弄清楚如何管理这个问题。正如我已经发现curses默认使用uft-8,而python字符串是用utf-16编码的,所以必须进行转换。当尝试从allchar 中逐一打印每个字符时,列出所有显示的字符。但我注意到,curses 中印有 [ 字符,这不在原始 plotext 输出中。我认为这是中间某个地方的 unicode 问题,但仍然找不到。任何帮助,将不胜感激。

    标签: python unicode terminal utf-8 python-curses


    【解决方案1】:

    我在这里遇到完全相同的问题。通过使用stransi 解决。您所需要的只是解析字符串,捕获颜色编码,在 curses 中重做 color_pair。顺便说一句,plotext 有一个 build() 方法,可以直接将其分配给变量,而无需使用 redirect_stdout。

    【讨论】:

      猜你喜欢
      • 2013-06-10
      • 2012-03-28
      • 2018-06-08
      • 2011-02-04
      • 2013-10-26
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多