gpt4 book ai didi

python - 值错误: invalid literal for int() with base 10: b'3\n1\n\n'

转载 作者:太空宇宙 更新时间:2023-11-03 20:58:45 25 4
gpt4 key购买 nike

我的代码有问题,我有一个 TCP 连接,我是客户端,并且我收到如下数字:

1
4
6
3
..

(我收到的数字只有:1、2、3、4、5、6、7),顺序随机。我收到的错误是: ValueError: invalidliteral for int() with base 10: b'2\n6\n\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)
#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

其中错误出现在第 44 行:i = int(s) 部分。您对我如何解决这个问题有什么想法吗?非常感谢

最佳答案

这里:

so.recv(port)

您收到了多个号码作为回复。尝试:

s = so.recv(port)
for i in [int(n) for n in s.split(b'\n') if n]:
...

关于python - 值错误: invalid literal for int() with base 10: b'3\n1\n\n',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55835105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com