python3 二进制编码问题

python3 issue when encoding binary

我是 Python3 的新手。 我正在编写一个允许我执行以下操作的程序:

TODO:将数字编码为二进制(基数 2)

我想知道为什么当我 运行 使用 Python2.7 工作正常的相同代码时,为什么会得到奇怪的输出。我对 encode('10',2) = '1010' 的预期输出但是,它打印出来了。

    423-e5323-e1323-e5.2323-e5223-e1223-e2223-e59.3223-e9.7123-e85.1123-e61.3123-e423.6023-e562.1023-e6925.2023-e950.5913-e58110.1913-e7320.2913-e4740.4913-e77490.8813-e459816.1813-e19732.3813-e718574.6713-e4361592.1713-e723095.2713-e456081.5613-e67031630.1613-e5162270.2613-e30325441.4613-e60640982.8513-e12908756.1513-e324816513.3513-e748632136.6413-e4963742623.1413-e7837494256.2413-e4774989403.5313-e5598799060.1313-e79097599121.2313-e39185199342.4313-e68361389784.8213-e77236695796.1213-e445562391593.3213-e90135683097.6113-e812603770853.1113-e5534216451617.2113-e178422903234.5013-e2479448164680.1013-e44849986329271.2013-e9698973748543.4013-e67397957496196.8903-e57859159833837.1903-e5719309776674.3903-e400538708553359.6803-e100765161176093.1803-e7100431323243187.2803-e300862646486265.5703-e7006352929635211.1703-e4102705858370522.2703-e304410717741054.4703-e608820434592009.8603-e167508680950087.1603-e225116371811065.3603-e440322743632021.7503-e906444962740424.1503-e812988835490848.2503-e634877770981696.5403-e1786555518732931.1403-e2473111136574872.2403-e4847222262159655.4403-e794544425209311.9303-e4990988405087228.1303-e991877900165546.3303-e793655910221192.7203-e5972119304422854.1203-e955228708844619.2203-e811546516798238.5103-e6320921325975661.1103-e740852640951333.2103-e490615290813666.4103-e981230581636233.9003-e8734600732725668.1003-e5578210474450337.3003-e157520849801664.7992-e2051506987122394.1992-e4003012975344689.2992-e106024851788279.5892-e2021486134775491.1892-e3042863368451983.2892-e184637627903877.4892-e169274354916655.9792-e3295496098323119.1792-e5481983187746228.3792-e963877265592546.7692-e8376555211950925.1692-e6743111522811850.3692-e596222054632611.6592-e935440092742322.1592-e870980085494644.2592-e651871061989298.4592-e213653023879587.9492-e5262170466591759.1492-e525241823193419.3492-e50582656287828.7392-e10752135657565.1392-e20415260315131.3392-e40820521620362.6292-e806500522506252.1292-e612110054012505.2292-e234220009024010.5192-e4684400081480200.1192-e8279800063861400.2192-e6549710027633800.4192-e198530044376610.8092-e2871700886433306.1092-e4653410673966602.3092-e317820257833314.6982-e6247504057766282.1982-e584118005533565.2982-e79226100176031.5882-e495423002431620.1882-e881946004862250.2882-e673892108635401.4882-e257695206370902.8782-e5053915027418146.1782-e107830144926382.3782-e204770288852765.6682-e4084514677154313.1682-e169038255309626.2682-e229166501708352.5582-e3483231124167050.1582-e6867462248225101.2582-e735925486540302.4582-e570950963190604.8482-e518118372812186.1482-e36326745634263.3482-e62742590378427.6382-e254940916479443.1382-e409890832949986.2382-e808791674898973.5282-e5165932596979570.1282-e321974093959151.2282-e642859087819303.4282-e294619165738706.8182-e5892383215765127.1182-e795667420531344.3182-e491335940072688.6082-e8836609900452773.1082-e6772318910805457.2082-e555626930610905.5972-e113529702308101.1972-e226058514606302.2972-e442107138212704.4972-e884204366524418.8872-e6794086231588267.1872-e599063562077525.3872-e99127035045150.7772-e893441601803014.1772-e697882212616028.2772-e295775424232146.5672-e5815519484642821.1672-e730138969294652.2672-e470266939589215.4672-e841423978179520.9572-e6928468573491508.1572-e956927157883016.3572-e813954305777022.7472-e7368196005551444.1472-e3727383100113888.2472-e554767200226677.5372-e194355004423551.1372-e289601108846013.2372-e469312206792126.4372-e729724402595242.9272-e5585588040915848.1272-e171177180830796.3272-e243245361670493.7172-e4864807232518874.1172-e739614564036759.2172-e478338039062519.5072-e7477661681250381.1072-e4945333273401663.2072-e990766447802237.4072-e891433984714464.9962-e5938668794388298.1962-e976337599667587.3962-e853764199335175.7862-e6174392897603415.1862-e349685695316820.3862-e788371391722750.6762-e3777436834544112.1762-e6455962778098224.2762-e901935457187548.4762-e812870905365196.9662-e7346518107213839.1662-e4782136304526678.3662-e575262708052357.7562-e515254161056055.1562-e30509223003101.3562-e60018546006202.6462-e210261921025042.1462-e420423852040184.2462-e840846615080269.4462-e690692330161429.9362-e912956602238489.1362-e834813314466969.3362-e778636628823939.7262-e3573723567568785.1262-e6057456035137571.3262-e105903160364153.6162-e2009162216292072.1162-e5008325422585045.2162-e106740944071180.5062-e2025908980432610.1062-e4040916971864230.2062-e180832953639460.4062-e261674817278921.8952-e3232596345479526.1952-e6464093780949152.3952-e929087471898305.6852-e9581659436977003.1852-e7173219962955106.2852-e347428935811302.5752-e7849469707326040.1752-e4798929514742180.2752-e597958138494261.4752-e95917366989423.8652-e819347239799466.1652-e638784568599923.3652-e276579037199956.6552-e3431591643899133.1552-e6862093296699366.2552-e735087483399723.5452-e5701659676895560.1452-e512219353791131.2452-e34428707493262.4452-e68846514987425.8352-e277921388759407.1352-e445952667519904.3352-e880915235138918.6252-e5718305603669363.1252-e536700316239727.2252-e72510622568554.5152-e450302540371190.1152-e801604090643281.2152-e612218081296463.4152-e234426163483927.8052-e4688423278678547.1052-e377946447357194.3052-e645992984705389.6942-e2909958794107693.1942-e3818917599204397.2942-e736934199508685.5842-e3729782891163711.1842-e7458575693227432.2842-e907151397444964.4842-e914303685988839.8742-e7386062719777787.1742-e4763125438555575.3742-e537240966111151.7642-e745808333222034.1642-e490716766444068.2642-e881432533988027.5542-e6738640768771441.1542-e576390437553882.2542-e53781864117675.4542-e7473639224351.9442-e49472785486038.1442-e88945471963166.3442-e67990943837223.7342-e259918966745464.1342-e4099369

查看下面的代码:

def encode(number, base):
    """Encode given number in base 10 to digits in given base.
    number: int -- integer representation of number (in base 10)
    base: int -- base to convert to
    return: str -- string representation of number (in given base)"""
    # Handle up to base 36 [0-9a-z]
    assert 2 <= base <= 36, 'base is out of range: {}'.format(base)
    # Handle unsigned numbers only for now
    assert number >= 0, 'number is negative: {}'.format(number)
    # TODO: Encode number in binary (base 2)
    # TODO: Encode number in hexadecimal (base 16)
    # TODO: Encode number in any base (2 up to 36)
    # it checks if the digit is digit else it's a letter.
    # create a helper function that handles hexadecimal digit_from_letter
    # we multify the number
    # new base num
    new_base_num = ''
    # encode helps me to make sure that we will work 
    while number != 0:
        remainder = number % base
        number = number / base
        if (remainder >= 10 and base > 10):
            remainder = letter_from_num(remainder) 
        else: 
            remainder
        new_base_num += str(remainder)
    # Reverse
    new_base_num = new_base_num[::-1]
    return new_base_num

    new_base_num = ''

    # # Loop through
    # while num != 0:
    #     remainder = num % base
    #     num = num / base
    #     remainder = letter_from_num(remainder) if (remainder >= 10 and base > 10) else remainder
    #     new_base_num += str(remainder)

    # # Reverse
    # new_base_num = new_base_num[::-1]

    # # Return
    # return new_base_num


def main():
    """Read command-line arguments and convert given digits between bases."""
    import sys
    args = sys.argv[1:]  # Ignore script file name
    if len(args) == 3:
        digits = args[0]
        #base1 = int(args[1])
        #base2 = int(args[2])
        # Convert given digits between bases
        #result = convert(digits, base1, base2)
        print('{} in base {} is {} in base {}'.format(digits, base1, result, base2))
    else:
        print('Usage: {} digits base1 base2'.format(sys.argv[0]))
        print('Converts digits from base1 to base2')


if __name__ == '__main__':
    main()
    print(encode(10,2))

当我输入 python file.py,

print(encode(10,2))

我们得到以下输出:"1010"

现在当我 运行 python3 file.py 它生成以下输出:

将数字从 base1 转换为 base2

423-e5323-e1323-e5.2323-e5223-e1223-e2223-e59.3223-e9.7123-e85.1123-e61.3123-e423.6023-e562.1023-e6925.2023-e950.5913-e58110.1913-e7320.2913-e4740.4913-e77490.8813-e459816.1813-e19732.3813-e718574.6713-e4361592.1713-e723095.2713-e456081.5613-e67031630.1613-e5162270.2613-e30325441.4613-e60640982.8513-e12908756.1513-e324816513.3513-e748632136.6413-e4963742623.1413-e7837494256.2413-e4774989403.5313-e5598799060.1313-e79097599121.2313-e39185199342.4313-e68361389784.8213-e77236695796.1213-e445562391593.3213-e90135683097.6113-e812603770853.1113-e5534216451617.2113-e178422903234.5013-e2479448164680.1013-e44849986329271.2013-e9698973748543.4013-e67397957496196.8903-e57859159833837.1903-e5719309776674.3903-e400538708553359.6803-e100765161176093.1803-e7100431323243187.2803-e300862646486265.5703-e7006352929635211.1703-e4102705858370522.2703-e304410717741054.4703-e608820434592009.8603-e167508680950087.1603-e225116371811065.3603-e440322743632021.7503-e906444962740424.1503-e812988835490848.2503-e634877770981696.5403-e1786555518732931.1403-e2473111136574872.2403-e4847222262159655.4403-e794544425209311.9303-e4990988405087228.1303-e991877900165546.3303-e793655910221192.7203-e5972119304422854.1203-e955228708844619.2203-e811546516798238.5103-e6320921325975661.1103-e740852640951333.2103-e490615290813666.4103-e981230581636233.9003-e8734600732725668.1003-e5578210474450337.3003-e157520849801664.7992-e2051506987122394.1992-e4003012975344689.2992-e106024851788279.5892-e2021486134775491.1892-e3042863368451983.2892-e184637627903877.4892-e169274354916655.9792-e3295496098323119.1792-e5481983187746228.3792-e963877265592546.7692-e8376555211950925.1692-e6743111522811850.3692-e596222054632611.6592-e935440092742322.1592-e870980085494644.2592-e651871061989298.4592-e213653023879587.9492-e5262170466591759.1492-e525241823193419.3492-e50582656287828.7392-e10752135657565.1392-e20415260315131.3392-e40820521620362.6292-e806500522506252.1292-e612110054012505.2292-e234220009024010.5192-e4684400081480200.1192-e8279800063861400.2192-e6549710027633800.4192-e198530044376610.8092-e2871700886433306.1092-e4653410673966602.3092-e317820257833314.6982-e6247504057766282.1982-e584118005533565.2982-e79226100176031.5882-e495423002431620.1882-e881946004862250.2882-e673892108635401.4882-e257695206370902.8782-e5053915027418146.1782-e107830144926382.3782-e204770288852765.6682-e4084514677154313.1682-e169038255309626.2682-e229166501708352.5582-e3483231124167050.1582-e6867462248225101.2582-e735925486540302.4582-e570950963190604.8482-e518118372812186.1482-e36326745634263.3482-e62742590378427.6382-e254940916479443.1382-e409890832949986.2382-e808791674898973.5282-e5165932596979570.1282-e321974093959151.2282-e642859087819303.4282-e294619165738706.8182-e5892383215765127.1182-e795667420531344.3182-e491335940072688.6082-e8836609900452773.1082-e6772318910805457.2082-e555626930610905.5972-e113529702308101.1972-e226058514606302.2972-e442107138212704.4972-e884204366524418.8872-e6794086231588267.1872-e599063562077525.3872-e99127035045150.7772-e893441601803014.1772-e697882212616028.2772-e295775424232146.5672-e5815519484642821.1672-e730138969294652.2672-e470266939589215.4672-e841423978179520.9572-e6928468573491508.1572-e956927157883016.3572-e813954305777022.7472-e7368196005551444.1472-e3727383100113888.2472-e554767200226677.5372-e194355004423551.1372-e289601108846013.2372-e469312206792126.4372-e729724402595242.9272-e5585588040915848.1272-e171177180830796.3272-e243245361670493.7172-e4864807232518874.1172-e739614564036759.2172-e478338039062519.5072-e7477661681250381.1072-e4945333273401663.2072-e990766447802237.4072-e891433984714464.9962-e5938668794388298.1962-e976337599667587.3962-e853764199335175.7862-e6174392897603415.1862-e349685695316820.3862-e788371391722750.6762-e3777436834544112.1762-e6455962778098224.2762-e901935457187548.4762-e812870905365196.9662-e7346518107213839.1662-e4782136304526678.3662-e575262708052357.7562-e515254161056055.1562-e30509223003101.3562-e60018546006202.6462-e210261921025042.1462-e420423852040184.2462-e840846615080269.4462-e690692330161429.9362-e912956602238489.1362-e834813314466969.3362-e778636628823939.7262-e3573723567568785.1262-e6057456035137571.3262-e105903160364153.6162-e2009162216292072.1162-e5008325422585045.2162-e106740944071180.5062-e2025908980432610.1062-e4040916971864230.2062-e180832953639460.4062-e261674817278921.8952-e3232596345479526.1952-e6464093780949152.3952-e929087471898305.6852-e9581659436977003.1852-e7173219962955106.2852-e347428935811302.5752-e7849469707326040.1752-e4798929514742180.2752-e597958138494261.4752-e95917366989423.8652-e819347239799466.1652-e638784568599923.3652-e276579037199956.6552-e3431591643899133.1552-e6862093296699366.2552-e735087483399723.5452-e5701659676895560.1452-e512219353791131.2452-e34428707493262.4452-e68846514987425.8352-e277921388759407.1352-e445952667519904.3352-e880915235138918.6252-e5718305603669363.1252-e536700316239727.2252-e72510622568554.5152-e450302540371190.1152-e801604090643281.2152-e612218081296463.4152-e234426163483927.8052-e4688423278678547.1052-e377946447357194.3052-e645992984705389.6942-e2909958794107693.1942-e3818917599204397.2942-e736934199508685.5842-e3729782891163711.1842-e7458575693227432.2842-e907151397444964.4842-e914303685988839.8742-e7386062719777787.1742-e4763125438555575.3742-e537240966111151.7642-e745808333222034.1642-e490716766444068.2642-e881432533988027.5542-e6738640768771441.1542-e576390437553882.2542-e53781864117675.4542-e7473639224351.9442-e49472785486038.1442-e88945471963166.3442-e67990943837223.7342-e259918966745464.1342-e4099369

我是编程新手,也是Python 3. 如果此提示没有帮助,请原谅。但也许这会回答你的问题。

Python 3.x 引入了一些 Python 2 不兼容的关键字和功能,可以通过内置的 future 模块导入Python 2. 如果您计划为您的代码提供Python 3.x 支持,建议使用future 导入它。比如我们想要Python 3.x在Python2中的整除行为,我们可以通过

导入
from __future__ import division

在这个 website

上找到