在python中用另一个字符替换一个字符

时间:2015-02-06 15:12:09

标签: python-2.7

我在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' 

1 个答案:

答案 0 :(得分:0)

,作为分隔符拆分字符串。然后将元素附加到列表

appended_list = [x for x in original_list.split(',')]

现在,附加列表将包含您希望的字符串