TW79
Posts: 36
Joined: Wed Jul 02, 2014 4:09 pm

Remove line breaks

Tue Aug 19, 2014 9:35 pm

All,

My script for creating a file is including line break from a script I can't figure out how to edit. I am trying to create a CSV file, and while the commas appear in the correct places, I can't seem to rid the final result of the \n lines.

Is there a way to remove the line breaks after the file has been created? Portion of script in questions below:

Code: Select all

import serial
ser = serial.Serial('/dev/ttyUSB0', 57600, timeout=100)
print ser.name
finaltags = open (gpsfile, 'a')
i = 0
while i < 50:
  x = ser.read(1)          # read one byte
  s = ser.read(10)        # read up to ten bytes (timeout)
  back = ('\b')
  print ser.readline()   # read a '\n' terminated line
  finaltags.write(ser.readline())
  finaltags.write(space)
  i = i + 1

finaltags.close()

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Remove line breaks

Wed Aug 20, 2014 11:32 am

this might do what you want.
the ".strip()" removes trailing CR and/or LF's which is what might be the problem.
not sure what "space" is, maybe comment out that line to see if that helps?
Also, are you sure you want to read 11 chars, then skip to the next EOL, then write the next line to file?

Code: Select all

import serial
ser = serial.Serial('/dev/ttyUSB0', 57600, timeout=100)
print ser.name
with open (gpsfile, 'a') as f
  for i in range(50): 
    ser.read(1)          # read one byte
    ser.read(10)        # read up to ten bytes (timeout)
    print ser.readline()   # read a '\n' terminated line
    f.write(ser.readline().strip() + '\n')
    f.write(space)
 

Return to “Python”