0

I finally wrote a program to recieve data from a socket:

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
   
 return
    
for line in readlines(sock):
 print line

I am recieving the required data line by line but there is a new line character at the end of each line which is not required for me.

Please tell me how to remove the character at the end of each line. I want to save these data to a database,line by line, in CSV format.

Regards

Manoj

Manoj P Joseph
  • 99
  • 1
  • 1
  • 5
  • possible duplicate of [How can I remove (chomp) a newline in Python?](http://stackoverflow.com/questions/275018/how-can-i-remove-chomp-a-newline-in-python) – Joe Feb 18 '15 at 10:16

1 Answers1

0

change

yield line

to

yield line.strip('\n')

if you are running on windows, you might need to do:

yield line.strip('\r\n')