- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Python 使用自定义颜色将日志/打印文本打印到终端。我环顾四周,但除了ANSI colors之外找不到任何东西。 .
我是否必须使用 ANSI 颜色,或者我是否能够使用自定义颜色(例如,使用 RGB 或十六进制值)将文本记录/打印到终端?
最佳答案
嗯,这是 Python 3 中的 256 色 xterm 调色板。来自 here 的转义码/十六进制值映射。 。 (我不记得为什么我称它为“aixterm” - 正如 Thomas Dickey 在评论中指出的那样,这实际上是针对 xterm 的。) The xterm
FAQ on colors也可能有帮助。
导入 myaixterm
,调用 aix_init()
,然后 print(aix_fg(128),'whatever',aix_normal())
。
它具有 aix_fg_rgb
和 aix_bg_rgb
函数,根据this .
# myaixterm.py: custom color mappings for the xterm 256-color palette
# or RGB888.
# Copyright (c) cxw 2015--2017
import itertools
import csv
_DEF_COLOR_FN='myaixterm-db.txt'
aix_colors={}
def aix_fg(color):
""" Returns a string that will set the foreground to _color_, which can
be a color number (0..255) or a name in aix_colors. """
if isinstance(color,str):
colornum=aix_colors[color]
else:
colornum=color
return '\033[38;5;%dm'%colornum
def aix_bg(color):
""" Returns a string that will set the background to _color_, which can
be a color number (0..255) or a name in aix_colors. """
if isinstance(color,str):
colornum=aix_colors[color]
else:
colornum=color
return '\033[48;5;%dm'%colornum
def aix_fg_rgb(r,g,b):
"""Returns a string to set foreground. r, g, b should be between 0 and 255."""
return '\033[38;2;%d;%d;%dm' % (r,g,b)
def aix_bg_rgb(r,g,b):
"""Returns a string to set background. r, g, b should be between 0 and 255."""
return '\033[48;2;%d;%d;%dm' % (r,g,b)
def aix_normal():
""" Returns a string that will set the foreground and background
to their default colors. """
return '\033[0m'
def aix_init(fn=_DEF_COLOR_FN):
with open(fn,'r') as fd:
reallines=itertools.filterfalse(lambda r: r.startswith('#'), fd)
for row in csv.DictReader(reallines,
fieldnames=['r','g','b','n'],
restkey='names'):
for name in row['names']:
aix_colors[name]=int(row['n'])
#end foreach name
#end foreach row
#end with
# end aix_init
以及相应的数据库,其中有我和 friend 添加的一些颜色名称:
# modified by cxw from colortest perl script
# at http://www.vim.org/scripts/script.php?script_id=1349
# original copyright:
# by entheon, do whatever the hell you want with this file
0,0,0,0,black,neutrala
191,0,0,1,red
0,191,0,2,green
191,191,0,3,yellow
0,0,191,4,blue
191,0,191,5,magenta
0,191,191,6,cyan
191,191,191,7,gray77,neutraly
64,64,64,8,gray27,neutralh
255,64,64,9,brightred
64,255,64,10,brightgreen
255,255,64,11,brightyellow
96,96,255,12,brightblue
255,64,255,13,brightmagenta
64,255,255,14,brightcyan
255,255,255,15,white,neutralzf
00,00,00,16,cubeblack
00,00,95,17,color17
00,00,135,18,color18
00,00,175,19,color19
00,00,215,20,color20
00,00,255,21,fullblue
00,95,00,22,color22
00,95,95,23,color23
00,95,135,24,color24
00,95,175,25,color25
00,95,215,26,color26
00,95,255,27,color27
00,135,00,28,color28
00,135,95,29,color29
00,135,135,30,color30
00,135,175,31,color31
00,135,215,32,color32
00,135,255,33,color33
00,175,00,34,color34
00,175,95,35,color35
00,175,135,36,color36
00,175,175,37,color37
00,175,215,38,color38
00,175,255,39,color39
00,215,00,40,color40
00,215,95,41,color41
00,215,135,42,color42
00,215,175,43,color43
00,215,215,44,color44
00,215,255,45,color45
00,255,00,46,fullgreen
00,255,95,47,color47
00,255,135,48,color48
00,255,175,49,color49
00,255,215,50,color50
00,255,255,51,fullcyan
95,00,00,52,color52
95,00,95,53,color53
95,00,135,54,color54
95,00,175,55,color55
95,00,215,56,color56
95,00,255,57,color57
95,95,00,58,color58
95,95,95,59,gray40,neutrall
95,95,135,60,color60
95,95,175,61,color61
95,95,215,62,color62
95,95,255,63,color63
95,135,00,64,color64
95,135,95,65,color65
95,135,135,66,color66
95,135,175,67,color67
95,135,215,68,color68
95,135,255,69,color69
95,175,00,70,color70
95,175,95,71,color71
95,175,135,72,color72
95,175,175,73,color73
95,175,215,74,color74
95,175,255,75,color75
95,215,00,76,color76
95,215,95,77,color77
95,215,135,78,color78
95,215,175,79,color79
95,215,215,80,color80
95,215,255,81,color81
95,255,00,82,color82
95,255,95,83,color83
95,255,135,84,color84
95,255,175,85,color85
95,255,215,86,color86
95,255,255,135,color135
135,00,00,88,color88
135,00,95,89,color89
135,00,135,90,color90
135,00,175,91,color91
135,00,215,92,color92
135,00,255,93,color93
135,95,00,94,color94
135,95,95,95,color95
135,95,135,96,color96
135,95,175,97,color97
135,95,215,98,color98
135,95,255,99,color99
135,135,00,100,color100
135,135,95,101,color101
135,135,135,102,gray56,neutralq
135,135,175,103,color103
135,135,215,104,color104
135,135,255,105,color105
135,175,00,106,color106
135,175,95,107,color107
135,175,135,108,color108
135,175,175,109,color109
135,175,215,110,color110
135,175,255,111,color111
135,215,00,112,color112
135,215,95,113,color113
135,215,135,114,color114
135,215,175,115,color115
135,215,215,116,color116
135,215,255,117,color117
135,255,00,118,color118
135,255,95,119,color119
135,255,135,120,color120
135,255,175,121,color121
135,255,215,122,color122
135,255,255,123,color123
175,00,00,124,color124
175,00,95,125,color125
175,00,135,126,color126
175,00,175,127,color127
175,00,215,128,color128
175,00,255,129,color129
175,95,00,130,color130
175,95,95,131,color131
175,95,135,132,color132
175,95,175,133,color133
175,95,215,134,color134
175,95,255,135,color135
175,135,00,136,color136
175,135,95,137,color137
175,135,135,138,color138
175,135,175,139,color139
175,135,215,140,color140
175,135,255,141,color141
175,175,00,142,color142
175,175,95,143,color143
175,175,135,144,color144
175,175,175,145,gray71,neutralv
175,175,215,146,color146
175,175,255,147,color147
175,215,00,148,color148
175,215,95,149,color149
175,215,135,150,color150
175,215,175,151,color151
175,215,215,152,color152
175,215,255,153,color153
175,255,00,154,color154
175,255,95,155,color155
175,255,135,156,color156
175,255,175,157,color157
175,255,215,158,color158
175,255,255,159,color159
215,00,00,160,color160
215,00,95,161,color161
215,00,135,162,color162
215,00,175,163,color163
215,00,215,164,color164
215,00,255,165,color165
215,95,00,166,color166
215,95,95,167,color167
215,95,135,168,color168
215,95,175,169,color169
215,95,215,170,color170
215,95,255,171,color171
215,135,00,172,color172
215,135,95,173,color173
215,135,135,174,color174
215,135,175,175,color175
215,135,215,176,color176
215,135,255,177,color177
215,175,00,178,color178
215,175,95,179,color179
215,175,135,180,color180
215,175,175,181,color181
215,175,215,182,color182
215,175,255,183,color183
215,215,00,184,color184
215,215,95,185,color185
215,215,135,186,color186
215,215,175,1135,color1135
215,215,215,188,gray86,neutralzb
215,215,255,189,color189
215,255,00,190,color190
215,255,95,191,color191
215,255,135,192,color192
215,255,175,193,color193
215,255,215,194,color194
215,255,255,195,color195
255,00,00,196,fullred
255,00,95,197,color197,awesome,rosebush
255,00,135,198,color198,hotpink
255,00,175,199,color199,beachtowel
255,00,215,200,color200,
255,00,255,201,fullmagenta
255,95,00,202,color202,fullorange
255,95,95,203,color203,melon
255,95,135,204,color204,petal
255,95,175,205,color205,pretty
255,95,215,206,color206
255,95,255,207,color207
255,135,00,208,color208,sunset
255,135,95,209,color209,flesh1
# ^^^ tan?
255,135,135,210,color210,lipstick
255,135,175,211,color211
255,135,215,212,color212
255,135,255,213,color213
255,175,00,214,color214,sun
255,175,95,215,color215,flesh2
255,175,135,216,color216,flesh3
255,175,175,217,color217,softpink,pillowpink
255,175,215,218,color218
255,175,255,219,color219
255,215,00,220,color220,sunrise
255,215,95,221,color221,pastelyellow
255,215,135,222,color222,flesh4
255,215,175,223,color223,flesh5
255,215,215,224,color224
255,215,255,225,color225
255,255,00,226,fullyellow
255,255,95,227,color227
255,255,135,228,color228
255,255,175,229,color229,parchment
255,255,215,230,color230,ivory
255,255,255,231,cubewhite,ultrawhite
8,8,8,232,gray2,neutralb
18,18,18,233,gray5,neutralc
28,28,28,234,gray10,neutrald
38,38,38,235,gray15,neutrale
48,48,48,236,gray20,neutralf
58,58,58,237,gray24,neutralg
68,68,68,238,gray29,neutrali
78,78,78,239,gray33,neutralj
88,88,88,240,gray37,neutralk
98,98,98,241,gray42,neutralm
108,108,108,242,gray46,neutraln
118,118,118,243,gray50,neutralo
128,128,128,244,gray54,neutralp
138,138,138,245,gray57,neutralr
148,148,148,246,gray61,neutrals
158,158,158,247,gray65,neutralt
168,168,168,248,gray69,neutralu
178,178,178,249,gray73,neutralw
188,188,188,250,gray76,neutralx
198,198,198,251,gray80,neutralz
208,208,208,252,gray83,neutralza
218,218,218,253,gray87,neutralzc
228,228,228,254,gray91,neutralzd
238,238,238,255,gray94,neutralze
关于python - 是否可以使用非 ANSI 颜色从 Python 进行打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46851356/
有没有更好的方法用 PHP 将数据输出到 html 页面? 如果我想在 php 中用一些 var 制作一个 div,我会写类似的东西 print (''.$var.''); 或 echo "''.$v
我可以使用 java awt print 来打印文档/文件而不是使用 javax print 吗?我发现在 java awt print 中有一个选项可以使用 AttributedString 将内容
目前我通过以下方式运行 R 脚本: R --slave argument1 argument2 ... 我想知道 R 中关于如何退出脚本并发出警告的最佳实践,q() 会这样做吗? if(!file.
谁能告诉我如何编写一个程序,用 gcc 编译时打印 c ,用 g++ 编译时打印 c++? 最佳答案 #ifdef __cplusplus printf("c++\n"); #else
我需要支持在 KitKat 设备上打印,但我的目标 SDK 是 13(无法更改)。 特别是我需要打印一个 webview。 这是用于打印 webview 的 API: http://developer
我正在尝试创建一个简单的函数,其中 python 将根据您的年份输入计算年龄。我已经尝试了几种方法,但我没有运气 atm。 附:对不起,我是新手。 ame = input(" Enter your n
JavaFX 2.0 是否支持打印?我有一个文本区域,我从中获取文本然后我想打印它,但似乎没有这个功能。 当然,这里我说的是打印到打印机。 :) 最佳答案 尚不支持。作为一种解决方法,您可以使用 Ja
我试图找出printOn的重点。我查看了一些实现它的类,看起来它只是帮助打印不同数据类型的单位。这是准确的吗? 如果是这样,有人能指出我如何为我自己的类(class)实现这一点的正确方向吗?我将在可能
我无法让 IE 打印我的 Canvas (使用 excanvas 生成)...我使用的是最新版本的 excanvas。 http://dl.dropbox.com/u/997831/canvas.ht
我搜索了很多但没有人回答我的问题,我读到在这样的信号处理程序中使用 cout 是不安全的: void ctrlZHandler(int sig_num) { //SIGTSTP-18
我有兴趣打印一系列查询。我有以下代码。 start = datetime.datetime(2012, 2, 2, 6, 35, 6, 764) end = datetime.datetime(201
public class javaClass { public static void main(String [] arg) { String row1 = "A____A"
我需要写入前一行的命令,例如不带\n 的 print()。 下面是一些示例代码: a=0 print("Random string value") if a==0: print_to_prev
我有一个使用 UIKit 和 Objective C 的旧 iOS 应用程序,我目前正在将其移植到 SwiftUI 和 Swift。一切都很顺利,我喜欢 Swift 和 SwiftUI。该应用程序已经
我创建了一个求和函数,它接受一个开始编号和一个结束编号,并返回这两点之间的总和答案 def print_sum_equations(start_number,end_number):
在 Perl 6 中,print 和有什么区别? , put和 say ? 我怎么看 print 5不同,但 put 5和 say 5看起来一样。 最佳答案 put $a就像 print $a.Str
我正在使用 here 中的 getOrgChart 库,我正在尝试打印整个图表,而不仅仅是可见部分。不幸的是,当使用标准库打印功能时,它只会打印出第一部分,而我不知道如何打印整个图表(该图表相当宽,大
我制作了一个非常适合 A4 页面的 View 。现在我想打印它。请注意,我没有使用drawRect或类似的东西,只是一个带有 subview 和文本标签的普通 View 。我的问题是,我对该 View
由于 Cocoa-Java 已弃用,我正在将 Cocoa-Java 代码迁移到 Cocoa + JNI。该代码打印存储在文件中的图像。新的 Cocoa 代码基本上是: NSImage *image =
这个问题已经有答案了: Printing a TDBGrid (4 个回答) 已关闭 6 年前。 如何在不安装或下载组件的情况下打印 DBGrid? 或者 如何将 DBGrid 的数据放入 RichE
我是一名优秀的程序员,十分优秀!