我如何从中完全删除colorama?

问题描述

我从一个朋友那里得到了这个代码,但是由于他在Linux上,所以其中包含了colorama,当我在那里使用颜色代码时,它对我不起作用,因此很难读取和/或复制和粘贴结果。我需要什么修改才能让colorama不在其中?

#!/usr/bin/python3
import requests
import json
import sys
import struct
import socket
import colorama
from colorama import Fore

indent = "  "


def ip2bin(ip):  # Credit to converter
    binary = bin(struct.unpack('!I',socket.inet_aton(ip))[0])
    return binary


def arg_length():
    return len(sys.argv)


def print_help():
    print("Commands:")
    print("** ipl -i <ip> - Gets information from an IP address")
    print("** ipl -h - displays this page.")


def lookup(ip):
    print("Looking up IP address \"" + str(ip) + "\"..")

    re = requests.get("https://ipinfo.io/" + str(ip) + "/json")

    j = json.loads(re.content)

    postal = ''
    loc = ''
    hostname = ''

    print("IP information:")

    for key,value in j.items():
        maininfo = True
        if str(key) == "error":
            print("Error: Invalid IP address")
            exit(0)
        if str(key) != "readme":
            if key == "postal":
                postal = value
                maininfo = False
            if key == "loc":
                loc = value
                maininfo = False
            if key == 'hostname':
                loc = value
                maininfo = False
            if maininfo:
                print(indent + key,"-",Fore.BLUE + value + Fore.WHITE)

    binary = ip2bin(ip).replace('0b','')

    ipClass = 'Class '

    if binary.startswith("0"):
        ipClass += 'A'

    elif binary.startswith('10'):
        ipClass += 'B'

    elif binary.startswith('110'):
        ipClass += 'C'

    elif binary.startswith('1110'):
        ipClass += 'D'

    elif binary.startswith('1111'):
        ipClass += 'E'

    print("Additional information:")
    print(indent + "IP Class: " + Fore.BLUE + ipClass + Fore.WHITE)
    print(indent + "Postal Code: " + Fore.BLUE + postal + Fore.WHITE)
    print(indent + "Long/Lat: " + Fore.BLUE + loc + Fore.WHITE)
    if not hostname == '':
        print("Hostname: " + hostname)


if arg_length() == 1 or arg_length() == 2:
    print_help()
if arg_length() == 3:
    if str(sys.argv[1]) == "-i":
        lookup(str(sys.argv[2]))
    else:
        print_help()

解决方法

只需删除对colorama和Fore的所有使用,就像这样:

#!/usr/bin/python3
import requests
import json
import sys
import struct
import socket

indent = "  "


def ip2bin(ip):  # Credit to converter
    binary = bin(struct.unpack('!I',socket.inet_aton(ip))[0])
    return binary


def arg_length():
    return len(sys.argv)


def print_help():
    print("Commands:")
    print("** ipl -i <ip> - Gets information from an IP address")
    print("** ipl -h - Displays this page.")


def lookup(ip):
    print("Looking up IP address \"" + str(ip) + "\"..")

    re = requests.get("https://ipinfo.io/" + str(ip) + "/json")

    j = json.loads(re.content)

    postal = ''
    loc = ''
    hostname = ''

    print("IP Information:")

    for key,value in j.items():
        maininfo = True
        if str(key) == "error":
            print("Error: Invalid IP address")
            exit(0)
        if str(key) != "readme":
            if key == "postal":
                postal = value
                maininfo = False
            if key == "loc":
                loc = value
                maininfo = False
            if key == 'hostname':
                loc = value
                maininfo = False
            if maininfo:
                print(indent + key,"-",value)

    binary = ip2bin(ip).replace('0b','')

    ipClass = 'Class '

    if binary.startswith("0"):
        ipClass += 'A'

    elif binary.startswith('10'):
        ipClass += 'B'

    elif binary.startswith('110'):
        ipClass += 'C'

    elif binary.startswith('1110'):
        ipClass += 'D'

    elif binary.startswith('1111'):
        ipClass += 'E'

    print("Additional Information:")
    print(indent + "IP Class: " + ipClass)
    print(indent + "Postal Code: " + postal)
    print(indent + "Long/Lat: " + loc)
    if not hostname == '':
        print("Hostname: " + hostname)


if arg_length() == 1 or arg_length() == 2:
    print_help()
if arg_length() == 3:
    if str(sys.argv[1]) == "-i":
        lookup(str(sys.argv[2]))
    else:
        print_help()

我认为人们不赞成您,因为您表现出缺乏努力。但是,我花了20秒钟来解决您的问题,所以我不介意回答。

最好使用我的答案,然后删除您的问题。

,

如果在导入colorama之后调用colorama的init函数,则该函数应为ensure that the output is Windows-compatible(假设您使用的是Windows)。

在Windows上,调用init()将从所有发送到stdout或stderr的文本中过滤出ANSI转义序列,并用等效的Win32调用替换它们。

import colormama
from colorama import Fore
from colorama import init

init()

# The rest of the module 
...

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...