• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

关闭

返回栏目

关闭

返回python栏目

38 - 自带方法 - print(pay.__dict__)打印obj内容

作者:

贺及楼

成为作者

更新日期:2024-06-08 22:34:26

print()打印

打印不换行

  1. print("\r"+str, end="")
  1. "\r"就是在首行开始打印
  2. end=""就是把打印的换行去掉

打印中间加符号

  1. print('cats', 'dogs', 'mice', sep = ',')
  2. 输出的是:cats,dogs,mice

颜色打印

  1. import ctypes,sys
  2. STD_INPUT_HANDLE = -10
  3. STD_OUTPUT_HANDLE = -11
  4. STD_ERROR_HANDLE = -12# 字体颜色定义 ,关键在于颜色编码,由2位十六进制组成,分别取0~f,前一位指的是背景色,后一位指的是字体色
  5. #由于该函数的限制,应该是只有这16种,可以前景色与背景色组合。也可以几种颜色通过或运算组合,组合后还是在这16种颜色中
  6. ## Windows CMD命令行 字体颜色定义 text colors
  7. FOREGROUND_BLACK = 0x00 # black.
  8. FOREGROUND_DARKBLUE = 0x01 # dark blue.
  9. FOREGROUND_DARKGREEN = 0x02 # dark green.
  10. FOREGROUND_DARKSKYBLUE = 0x03 # dark skyblue.
  11. FOREGROUND_DARKRED = 0x04 # dark red.
  12. FOREGROUND_DARKPINK = 0x05 # dark pink.
  13. FOREGROUND_DARKYELLOW = 0x06 # dark yellow.
  14. FOREGROUND_DARKWHITE = 0x07 # dark white.
  15. FOREGROUND_DARKGRAY = 0x08 # dark gray.
  16. FOREGROUND_BLUE = 0x09 # blue.
  17. FOREGROUND_GREEN = 0x0a # green.
  18. FOREGROUND_SKYBLUE = 0x0b # skyblue.
  19. FOREGROUND_RED = 0x0c # red.
  20. FOREGROUND_PINK = 0x0d # pink.
  21. FOREGROUND_YELLOW = 0x0e # yellow.
  22. FOREGROUND_WHITE = 0x0f # white.# Windows CMD命令行 背景颜色定义 background colors
  23. BACKGROUND_BLUE = 0x10 # dark blue.
  24. BACKGROUND_GREEN = 0x20 # dark green.
  25. BACKGROUND_DARKSKYBLUE = 0x30 # dark skyblue.
  26. BACKGROUND_DARKRED = 0x40 # dark red.
  27. BACKGROUND_DARKPINK = 0x50 # dark pink.
  28. BACKGROUND_DARKYELLOW = 0x60 # dark yellow.
  29. BACKGROUND_DARKWHITE = 0x70 # dark white.
  30. BACKGROUND_DARKGRAY = 0x80 # dark gray.
  31. BACKGROUND_BLUE = 0x90 # blue.
  32. BACKGROUND_GREEN = 0xa0 # green.
  33. BACKGROUND_SKYBLUE = 0xb0 # skyblue.
  34. BACKGROUND_RED = 0xc0 # red.
  35. BACKGROUND_PINK = 0xd0 # pink.
  36. BACKGROUND_YELLOW = 0xe0 # yellow.
  37. BACKGROUND_WHITE = 0xf0 # white.# get handle
  38. std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
  39. def set_cmd_text_color(color, handle=std_out_handle):
  40. Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
  41. return Bool#reset white
  42. def resetColor():
  43. set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
  44. ################################################################暗蓝色
  45. #dark blue
  46. def printDarkBlue(mess):
  47. set_cmd_text_color(FOREGROUND_DARKBLUE)
  48. sys.stdout.write(mess)
  49. resetColor()#暗绿色
  50. #dark green
  51. def printDarkGreen(mess):
  52. set_cmd_text_color(FOREGROUND_DARKGREEN)
  53. sys.stdout.write(mess)
  54. resetColor()#暗天蓝色
  55. #dark sky blue
  56. def printDarkSkyBlue(mess):
  57. set_cmd_text_color(FOREGROUND_DARKSKYBLUE)
  58. sys.stdout.write(mess)
  59. resetColor()#暗红色
  60. #dark red
  61. def printDarkRed(mess):
  62. set_cmd_text_color(FOREGROUND_DARKRED)
  63. sys.stdout.write(mess)
  64. resetColor()#暗粉红色
  65. #dark pink
  66. def printDarkPink(mess):
  67. set_cmd_text_color(FOREGROUND_DARKPINK)
  68. sys.stdout.write(mess)
  69. resetColor()#暗黄色
  70. #dark yellow
  71. def printDarkYellow(mess):
  72. set_cmd_text_color(FOREGROUND_DARKYELLOW)
  73. sys.stdout.write(mess)
  74. resetColor()#暗白色
  75. #dark white
  76. def printDarkWhite(mess):
  77. set_cmd_text_color(FOREGROUND_DARKWHITE)
  78. sys.stdout.write(mess)
  79. resetColor()#暗灰色
  80. #dark gray
  81. def printDarkGray(mess):
  82. set_cmd_text_color(FOREGROUND_DARKGRAY)
  83. sys.stdout.write(mess)
  84. resetColor()#蓝色
  85. #blue
  86. def printBlue(mess):
  87. set_cmd_text_color(FOREGROUND_BLUE)
  88. sys.stdout.write(mess)
  89. resetColor()#绿色
  90. #green
  91. def printGreen(mess):
  92. set_cmd_text_color(FOREGROUND_GREEN)
  93. sys.stdout.write(mess)
  94. resetColor()#天蓝色
  95. #sky blue
  96. def printSkyBlue(mess):
  97. set_cmd_text_color(FOREGROUND_SKYBLUE)
  98. sys.stdout.write(mess)
  99. resetColor()#红色
  100. #red
  101. def printRed(mess):
  102. set_cmd_text_color(FOREGROUND_RED)
  103. sys.stdout.write(mess)
  104. resetColor()#粉红色
  105. #pink
  106. def printPink(mess):
  107. set_cmd_text_color(FOREGROUND_PINK)
  108. sys.stdout.write(mess)
  109. resetColor()#黄色
  110. #yellow
  111. def printYellow(mess):
  112. set_cmd_text_color(FOREGROUND_YELLOW)
  113. sys.stdout.write(mess)
  114. resetColor()#白色
  115. #white
  116. def printWhite(mess):
  117. set_cmd_text_color(FOREGROUND_WHITE)
  118. sys.stdout.write(mess)
  119. resetColor()###################################################白底黑字
  120. #white bkground and black text
  121. def printWhiteBlack(mess):
  122. set_cmd_text_color(FOREGROUND_BLACK | BACKGROUND_WHITE)
  123. sys.stdout.write(mess)
  124. resetColor()#白底黑字
  125. #white bkground and black text
  126. def printWhiteBlack_2(mess):
  127. set_cmd_text_color(0xf0)
  128. sys.stdout.write(mess)
  129. resetColor()#黄底蓝字
  130. #white bkground and black text
  131. def printYellowRed(mess):
  132. set_cmd_text_color(BACKGROUND_YELLOW | FOREGROUND_RED)
  133. sys.stdout.write(mess)
  134. resetColor()##############################################################
  135. if __name__ == '__main__':
  136. # print
  137. printDarkBlue(u'printDarkBlue:暗蓝色文字\n')
  138. printDarkGreen(u'printDarkGreen:暗绿色文字\n')
  139. printDarkSkyBlue(u'printDarkSkyBlue:暗天蓝色文字\n')
  140. printDarkRed(u'printDarkRed:暗红色文字\n')
  141. printDarkPink(u'printDarkPink:暗粉红色文字\n')
  142. printDarkYellow(u'printDarkYellow:暗黄色文字\n')
  143. printDarkWhite(u'printDarkWhite:暗白色文字\n')
  144. printDarkGray(u'printDarkGray:暗灰色文字\n')
  145. printBlue(u'printBlue:蓝色文字\n')
  146. printGreen(u'printGreen:绿色文字\n')
  147. printSkyBlue(u'printSkyBlue:天蓝色文字\n')
  148. printRed(u'printRed:红色文字\n')
  149. printPink(u'printPink:粉红色文字\n')
  150. printYellow(u'printYellow:黄色文字\n')
  151. printWhite(u'printWhite:白色文字\n')
  152. printWhiteBlack(u'printWhiteBlack:白底黑字输出\n')
  153. printWhiteBlack_2(u'printWhiteBlack_2:白底黑字输出\n')
  154. printYellowRed(u'printYellowRed:黄底红字输出\n')