Python 我如何从中完全去除colorama?

Python 我如何从中完全去除colorama?,python,colorama,Python,Colorama,我从一个朋友那里得到了这段代码,但因为他在linux上,所以他有colorama,这对我来说不起作用,当颜色代码正确时,我很难读取和/或复制粘贴结果。我需要编辑什么才能使colorama不在其中 #!/usr/bin/python3 import requests import json import sys import struct import socket import colorama from colorama import Fore indent = " "

我从一个朋友那里得到了这段代码,但因为他在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()
#/usr/bin/python3
导入请求
导入json
导入系统
导入结构
导入套接字
进口色拉马
从colorama进口
缩进=“”
def ip2bin(ip):#归功于转换器
binary=bin(结构解包('!I',socket.inet_aton(ip))[0])
返回二进制
def arg_length():
返回长度(系统argv)
def print_help():
打印(“命令:”)
打印(“**ipl-i-从IP地址获取信息”)
打印(“**ipl-h-显示此页面。”)
def查找(ip):
打印(“查找IP地址\”“+str(IP)+”“。”)
re=请求。获取(“https://ipinfo.io/“+str(ip)+“/json”)
j=json.loads(关于内容)
邮政=“”
loc=“”
主机名=“”
打印(“IP信息:”)
对于键,j.items()中的值:
mainfo=True
如果str(键)=“错误”:
打印(“错误:无效IP地址”)
出口(0)
如果str(键)!=“自述文件”:
如果键==“邮政”:
邮政=价值
mainfo=False
如果键==“loc”:
loc=值
mainfo=False
如果键=='hostname':
loc=值
mainfo=False
如果maininfo:
打印(缩进+键“-”,前蓝+值+前白)
二进制=ip2bin(ip)。替换('0b','')
ipClass='Class'
如果二进制.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”
打印(“其他信息:”)
打印(缩进+“IP类:”+Fore.BLUE+ipClass+Fore.WHITE)
打印(缩进+邮政编码:“+Fore.BLUE+Postal+Fore.WHITE”)
打印(缩进+“长/横向:”+Fore.BLUE+loc+Fore.WHITE)
如果不是主机名=='':
打印(“主机名:”+主机名)
如果arg_length()==1或arg_length()==2:
打印帮助()
如果arg_length()=3:
如果str(sys.argv[1])==“-i”:
查找(str(sys.argv[2]))
其他:
打印帮助()

只需删除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()
#/usr/bin/python3
导入请求
导入json
导入系统
导入结构
导入套接字
缩进=“”
def ip2bin(ip):#归功于转换器
binary=bin(结构解包('!I',socket.inet_aton(ip))[0])
返回二进制
def arg_length():
返回长度(系统argv)
def print_help():
打印(“命令:”)
打印(“**ipl-i-从IP地址获取信息”)
打印(“**ipl-h-显示此页面。”)
def查找(ip):
打印(“查找IP地址\”“+str(IP)+”“。”)
re=请求。获取(“https://ipinfo.io/“+str(ip)+“/json”)
j=json.loads(关于内容)
邮政=“”
loc=“”
主机名=“”
打印(“IP信息:”)
对于键,j.items()中的值:
mainfo=True
如果str(键)=“错误”:
打印(“错误:无效IP地址”)
出口(0)
如果str(键)!=“自述文件”:
如果键==“邮政”:
邮政=价值
mainfo=False
如果键==“loc”:
loc=值
mainfo=False
如果键=='hostname':
loc=值
mainfo=False
如果maininfo:
打印(缩进+键“-”,值)
二进制=ip2bin(ip)。替换('0b','')
ipClass='Class'
如果二进制.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”
打印(“其他信息:”)
打印(缩进+“IP类:”+ipClass)
打印(缩进+邮政编码:“+邮政编码”)
打印(缩进+长/横向:+loc)
如果不是主机名=='':
打印(“主机名:”+主机名)
如果arg_length()==1或arg_length()==2:
打印帮助()
如果arg_length()=3:
如果str(sys.argv[1])==“-i”:
查找(str(sys.argv[2]))
其他:
打印帮助()
我认为人们投票否决你是因为你表现出缺乏努力。然而,我花了20秒来解决你的问题,所以我不介意回答


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

如果您在导入colorama后调用colorama的
init
函数,则应该调用它(假设您在Windows上)

在Windows上,调用init()将从发送到stdout或stderr的任何文本中筛选出ANSI转义序列,并用等效的Win32调用替换它们


我想你可以自己解决这个问题,如果你删除了colorama模块(及其子模块),你会看到一些错误行,然后你可以按照你想要的方式修复这些行。
import colormama
from colorama import Fore
from colorama import init

init()

# The rest of the module 
...