ValueError:int()的基数为10的无效文字:b'1 \ n5 \ n'

问题描述 投票:1回答:1

我在python中有一个程序,通过TCP-IP连接到一个matlab程序,其中python代码是客户端,其接收数字如下:

1
2
5
6
7
etc..

(我收到的数字只是:1,2,3,4,5,6,7)按随机顺序排列。我得到的错误是:ValueError:int()的基数为10的无效文字:b'1 \ n5 \ n'。我的代码是:

# TCP connection
try:
    so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as err:
    print ("socket creation failed with error %s" %(err))

#default port for socket
port = 2000

# default time out
#so.settimeout(1000000)

try:
    host_ip = socket.gethostbyname('localhost')
except socket.gaierror:
    # this means could not resolve the host
    print ("there was an error resolving the host")
    sys.exit()

# connecting to the server
so.connect((host_ip,port))

# MATLAB INFORMATION FOR OFFLINE EXPERIMENT
Nepoch = 10  #nr de epochs por trial
Nwords = 7   #nr de palavras (SIM, NAO, FOME, SEDE, URINAR, AR, POSICAO)
SeqTrain = [1, 3, 5, 7, 2, 4, 6, 1, 3, 5, 7, 2, 4, 6] #sequencia offline de treino

# read the TCP sequence received
def sequencia():
    num = 0
    for i in range(0,999):
        s = so.recv(port) #+ b'\n' #since the sequence received is : 1\n 2\n 5\n etc
        i = int(s)
        #print(i)
        #feedbak offline (for the user to know which are the words)
        if (num in (0, Nepoch*Nwords+1, Nepoch*Nwords*2+2, Nepoch*Nwords*3+3, Nepoch*Nwords*4+4, Nepoch*Nwords*5+5,\
                    Nepoch*Nwords*6+6)):
            labels1[i-1].configure(foreground="white")
            root.update()
        elif (num in (Nepoch*Nwords*7+7, Nepoch*Nwords*8+8, Nepoch*Nwords*9+9, Nepoch*Nwords*10+10,\
                     Nepoch*Nwords*11+11, Nepoch*Nwords*12+12, Nepoch*Nwords*13+13)):
            labels2[i-1].configure(foreground="white")
            root.update()
        else:
            labels[i-1].configure(background="green",foreground="red")
            root.update()
            winsound.PlaySound(sounds[i-1], winsound.SND_FILENAME)
            labels[i-1].configure(background="gray",foreground="white")
            root.update()
        num = num + 1

我收到的数字是在matlab程序中实时生成的。事情就是当我在matlab中使用标准值进行模拟时,python程序运行得很好,这让我相信这是因为matlab中生成的实时值。

另外,当我将#feedbak的部分注释掉(让用户知道哪些是单词)直到结束时,程序接收数字并且i = int(s)没有任何问题,只有当我取消注释其余部分时给了我错误。当我打印我收到的值时,如:b'1 \ n'b'7 \ n'b'4 \ n'b'2 \ n'b'6 \ n'b'3 \ n'b '1 \ n'b'5 \ n'(等等) - >它从不说它同时收到2个值,就像我取消注释程序的其余部分一样

我发布的所有python程序,适用于前2/3数字,然后给我错误,这里是追溯:

>>> 
 RESTART: C:\Users\meca\Desktop\Python_Exercises\seq_tcp_offline-TCP-CLIENT.py 
b'1\n'
b'7\n'
b'4\n'
Traceback (most recent call last):
  File "C:\Users\meca\Desktop\Python_Exercises\seq_tcp_offline-TCP-CLIENT.py", line 139, in <module>
    sequencia()
  File "C:\Users\meca\Desktop\Python_Exercises\seq_tcp_offline-TCP-CLIENT.py", line 44, in sequencia
    i = int(s)
ValueError: invalid literal for int() with base 10: b'2\n6\n'

这对我来说很奇怪,你们有什么想法吗?非常感谢

python tcp real-time
1个回答
0
投票

\n是一个换行符。当bytes对象中有多个换行符时,int()遇到问题。

可以转换单个数字后跟换行符。

>>> b = b'1\n'
>>> int(b)
1

当您收到由换行符分隔的数字流时,您需要在转换之前将空格上的字节对象拆分。

>>> b = b'1\n5\n'
>>> b.split()
[b'1', b'5']
>>> for c in b.split():
...     print(int(c))

1
5
>>>

要么

>>> [int(n) for n in b.split()]
[1, 5]
>>> 

或者,您可以尝试在每次迭代时只读取2个字节。目前,您将2000的值传递给buffsize参数。

s = so.recv(2)

您可能希望对此进行测试以确保不使用数据 - 如果您一次只读取2个字节,我不知道套接字如何处理堆积的数据。 。

© www.soinside.com 2019 - 2024. All rights reserved.