如何在执行前通过 python 程序显式配置命令提示符?

How to configure command prompt explicitly via a python program before its execution?

我目前正在处理我的 School 项目,我创建了一个模块,可以输入嵌套列表,然后转换为 table(或者恰好是看起来像 table 的字符串).

#Working of program
nested_list = nested_list = [['This', 'is' , 'a' , 'demo'], ['Hello', 'How', 'are'], ['You']]

#Creating table (doc string)
#the program processes the nested list to generate a doc string like the following
table = """
+-------------+-------------+-------------+-------------+
|    This     |     is      |      a      |    demo     |
+-------------+-------------+-------------+-------------+
|    Hello    |     How     |     are     |      -      |
+-------------+-------------+-------------+-------------+
|     You     |      -      |      -      |      -      |
+-------------+-------------+-------------+-------------+ """
#when printed it looks like a table.

问题是它只适用于等宽字体,而且当字符串被换行时看起来很糟糕。 我可以修复它的唯一方法是通过程序本身显式配置命令提示符 window 字体的字体、高度、宽度和颜色。

如何在 Python 3.7.4(我目前使用的 python 版本)中做到这一点?

我目前正在使用 Windows 10,我必须 运行 命令提示符下的程序(当然!)。

我认为您不会在控制台中看到 non-monospaced 字体。但是,控制台 window 的宽度和高度可能是个问题 - 在 Windows 中,您可以通过 运行ning mode width, height 更改控制台 window 的大小] 例如 mode 120,40。您也可以从 Python 脚本发出该命令。

import os

os.system('mode 180,20')
print('Hello wide world!')
os.system('pause')

此示例展示了您想要的效果。

请注意,这不会影响特定的控制台,例如,您的脚本将 运行 来自 IDE 的控制台,例如 PyCharm - 它假设您的代码是 运行在普通 Windows 控制台中 window。

一般来说,我不建议使用此类解决方案。它们使您的脚本特定于特定的 OS 和特定的 运行 方式。提出几乎可以在任何地方使用的解决方案通常会更好。