-4
我在python中使用以下代碼接收來自設備的數據。替換python中的另一個字符
from socket import *
HOST = 'localhost'
PORT = 30003 #our port from before
ADDR = (HOST,PORT)
BUFSIZE = 4096
sock = socket(AF_INET,SOCK_STREAM)
sock.connect((ADDR))
def readlines(sock, recv_buffer=4096, delim='\n'):
buffer = ''
data = True
while data:
data = sock.recv(recv_buffer)
buffer += data
while buffer.find(delim) != -1:
line, buffer = buffer.split('\n', 1)
yield line.strip('\r\n')
return
for line in readlines(sock):
print line
而且我收到輸出格式如下:
MSG,2,0,0,8963AB,0,2015/02/06,15:03:27.380,2015/02/06,15:03:27.380,,0,7.5,343.0,10.152763,76.390593,,,,,,-1 MSG,2,0,0,8963AB,0,2015/02/06,15:03:28.630,2015/02/06,15:03:28.630,,0,7.5,348.0,10.152809,76.390593,,,,,,-1
我應該得到以下格式的輸出:
'MSG','2','0','0','8963AB','0','2015/02/06','15:03:27.380','2015/02/06','15:03:27.380','','0','7.5','343.0','10.152763','76.390593','','','','','','-1'
對不起,我在python初學者。這些數據持續流式傳輸數據和文件。所以我希望每一行都有新的格式。所以在哪裏我應該添加你的代碼。 – 2015-02-07 15:01:10