Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
int(),can';t在python中使用显式基转换非字符串_Python - Fatal编程技术网

int(),can';t在python中使用显式基转换非字符串

int(),can';t在python中使用显式基转换非字符串,python,Python,有两件事我不明白: 1.“非字符串-带显式基址”是什么意思?它像指针吗? 2如何解决 您可以检查代码及其在第25行出现的错误。(可点击) 错误消息: dec_list.append(int(rmsg[x],2)) TypeError: int() can't convert non-string with explicit base 很明显,我不是一个python人,但我时常尝试做smth 我的aaaa.txt文件包含 011100101000010101010101000010101010

有两件事我不明白:

1.“非字符串-带显式基址”是什么意思?它像指针吗? 2如何解决

您可以检查代码及其在第25行出现的错误。(可点击)

错误消息:

dec_list.append(int(rmsg[x],2)) 
TypeError: int() can't convert non-string with explicit base
很明显,我不是一个python人,但我时常尝试做smth

我的aaaa.txt文件包含
0111001010000101010101010000101010101010110011011010101010101010100100100100001010101010100100101000000101010010101010000001010100101011001

    local_file=open('aaaa.txt' , 'r', encoding = 'utf-8') 
    #1st checkpoint open and read the file
        a=local_file.read()
    n=10
    i=0
    msg=list() #create a blank list for lsb first
    rmsg=list() #create a blank list for msb first
    asciimsg=list() #create a blank list for decoded ASCII character
    def swap(num):
        str_num = str(num)
   

 str_swapped = str_num[-1] +str_num[-2] + str_num[2:-2]  + str_num[1] + str_num[0]
    return int(str_swapped)


    while (n<=len(a)):
# 3th cp: gets the 7 bit, lsb first in the aaaa.txt, one by one till the end
        msg.append(a[i+1:n-2])  
        rmsg.append(swap(a[i+1:n-2]))
        n=n+10
        i=i+10
    print('lsb first binary:')
    print(msg)
    print('msb first binary(ASCII):')
    print(rmsg)
    dec_list=list()
    for x in range(len(rmsg)):
       dec_list.append(int(rmsg[x],2)) #4th checkpoint: convert to decimal
    print('ASCII in decimal:')
    print(dec_list)
    for z in dec_list:
        asciimsg.append(chr(z)) #5th cp: use the chr function to convert to ascii
    print(asciimsg)
    shortmsg=""
    for t in asciimsg:
        shortmsg=shortmsg+t
    print(shortmsg) #6th cp: printing the decoded msg
local\u file=open('aaaa.txt','r',encoding='utf-8')
#第一个检查点打开并读取文件
a=本地_文件.read()
n=10
i=0
msg=list()#首先为lsb创建一个空白列表
rmsg=list()#首先为msb创建一个空白列表
asciimsg=list()#为解码的ASCII字符创建一个空白列表
def交换(num):
str_num=str(num)
str_swapped=str_num[-1]+str_num[-2]+str_num[2:-2]+str_num[1]+str_num[0]
返回整数(str_交换)

虽然(n
int
不是
str
,但请先将其转换为字符串:

dec_list.append(int(str(rmsg[x]), 2)) 
使用列表理解而不是像那样的for循环更符合Python,不过:

dec_list = [int(str(c), 2) for c in rmsg]

int()是python中用于将字符串形式的数字转换为整数的方法,例如:“2”是字符串,int(“2”)=2,因此现在2是整数,与您的情况相同,但基本情况是消息的初始形式既不是整数也不是字符串,因此首先需要使用str(message)将消息转换为字符串然后是int(message),也称为int(str(message)).

如果你给一个基数,你也必须把你的数字作为它在这个基数中的文本表示形式来传递。但是很明显,你传递的甚至不是一个字符串,因此是错误的。如果你不给一个基数,第一个参数可以是其他参数,比如一个浮点数;别介意我只是写了一些其他的东西。谢谢你。很抱歉,但是你可能会这样做没有。我的意思是,
rmsg[x]
应该是一个字符串,表示一个有效的基数2。当然不是。在转换之前,只需尝试
print(rmsg[x])
,看看它是什么。如果要转换到另一个基数,在它周围放一个
str(…)
不会解决问题(你是),那么第一个参数必须是一个字符串(它不是)。所以基本上,当我写int()或str()时,如果你有java方面的知识,它类似于java中的向下转换。谢谢你基本上是的,它类似于向下转换,没问题!他们链接了问题中的代码(查找单词“here”),所以很明显,它实际上是这样的整数。