11

This is my first python program and my first asking a question on stack overflow so I apologize if my code is a mess and or if my question is ill-formatted.

I would like to print the same line I'm already printing, but each float should be a different color based on its value. (specifically >.7 is green, .7< is red) What is the best way to do this?

oreName=[#string names]

#escape char? I know there has to be a better way than this
#but this is the best ive come up with as the escape char didnt
#work the way I thought it should for '%'
char = '%'

netProfitBroker=[
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]]

##code that populates netProfitBroker

def printOutput(array):
  "this prints all of the lines"
  for i in range(0,10):
    print oreName[i]+"=   %.3f \t 5"%(array[0][i])+char+"=%.3f \t10"%(array[1][i])+char+"=%.3f"%(array[2][i])    


print "\nnet profit brokered"
printOutput(netProfitBroker)

the output looks a bit like this: (I lost some/all of my whitespace formatting when I copied the output here)

net profit brokered

Veldspar   =   0.234     5%=0.340   10%=-0.017
Scordite   =   0.752     5%=0.297   10%=0.259
Pyroxeres  =   0.406     5%=1.612   10%=2.483
Plagioclase=   1.078     5%=0.103   10%=1.780
Omber      =   -7.120    5%=5.416   10%=4.612
Kernite    =   -10.822   5%=15.366  10%=6.626
Jaspet     =   17.772    5%=49.278  10%=62.380
Hemorphite =   -35.431   5%=82.912  10%=141.027
Gneiss     =   8.086     5%=-4638.549   10%=-3610.570
Arkonor    =   349.867   5%=-545.284    10%=-340.298

essentially:

"ore name=" arrayVal1 "5%="arrayVal2 "10%="arrayVal3

All the array vals should be printed out to 3 decimal places.

Slicedbread
  • 2,208
  • 3
  • 21
  • 28

2 Answers2

34

You can use ASCII color codes at the beginning of your print to change the color, as an example '\033[91m' for RED and '\033[94m' for BLUE.

e.g

if array[0][i] > 7:
   print '\033[94m' + oreName[i]+"=   %.3f \t 5"%(array[0][i])
elif array[0][i] < 7:
   print '\033[91m' + oreName[i]+"=   %.3f \t 5"%(array[0][i])

You can read about ASCII escape codes here.

Edit: Added additional example code.

Here is a list of some common colors you can use:

Red = '\033[91m'
Green = '\033[92m'
Blue = '\033[94m'
Cyan = '\033[96m'
White = '\033[97m'
Yellow = '\033[93m'
Magenta = '\033[95m'
Grey = '\033[90m'
Black = '\033[90m'
Default = '\033[99m'

Additionally as mentioned in the comment. You can chain these to get different colors on the same line.

print '\033[91m' + 'Red' + "\033[99m" + 'Normal' + '\033[94m' + 'Blue

You could even do a function.

# Store a dictionary of colors.
COLOR = {
    'blue': '\033[94m',
    'default': '\033[99m',
    'grey': '\033[90m',
    'yellow': '\033[93m',
    'black': '\033[90m',
    'cyan': '\033[96m',
    'green': '\033[92m',
    'magenta': '\033[95m',
    'white': '\033[97m',
    'red': '\033[91m'
}


def print_with_color(message, color='red'):
    print(COLOR.get(color.lower(), COLOR['default']) + message)


print_with_color('hello colorful world!', 'magenta')
print_with_color('hello colorful world!', 'blue')
eandersson
  • 25,781
  • 8
  • 89
  • 110
  • This recolors the entire line. What I was hoping to do was recolor just the floats. Each line has three floats that each need their own color. – Slicedbread Mar 23 '13 at 02:21
  • 2
    You can simply combine them e.g. `print '\033[91m' + 'Red' + "\033[99m" + 'Normal' + print '\033[94m' + 'Blue` – eandersson Mar 23 '13 at 02:39
  • Here's a more comprehensive list used in golang https://godoc.org/github.com/whitedevops/colors – Daniel F Sep 07 '19 at 16:13
0

for Easy copy-paste after print:

for style in range(8):
    for fg in range(30,38):
        s1 = ''
        for bg in range(40,48):
            myform = ';'.join([str(style), str(fg), str(bg)])
            skel = '\x1b[{};{};{}m'.format(myform.split(';')[0],myform.split(';')[1],myform.split(';')[2])
            s1 += f'\x1b[{myform}m {repr(skel)} \x1b[0m'
        print(s1)
    print('\n')

This code print multiple pretty lines with different colors. example: '\x1b[5;31;40m' '\x1b[5;31;41m' '\x1b[5;31;42m' '\x1b[5;31;43m' for easy copy-paste to code. After print add to you code:

mycolor = '\x1b[5;35;43m'
rest    = '\x1b[0m'

and print for preview:

print(f"{mycolor}Hello world!{rest}")

for preview: Stack attach

Kasumiru
  • 1
  • 1
  • 1