473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

getting rid of EOL character ?

hello,

In the previous language I used,
when reading a line by readline, the EOL character was removed.

Now I'm reading a text-file with CR+LF at the end of each line,
Datafile = open(filename,'r')
line = Datafile.readline()

now this gives an extra empty line
print line

and what I expect that should be correct, remove CR+LF,
gives me one character too much removed
print line[,-2]

while this gives what I need ???
print line[,-1]

Is it correct that the 2 characters CR+LF are converted to 1 character ?
Is there a more automatic way to remove the EOL from the string ?

thanks,
Stef Mientki
Apr 27 '07 #1
7 13884
stef wrote:
hello,

In the previous language I used,
when reading a line by readline, the EOL character was removed.

Now I'm reading a text-file with CR+LF at the end of each line,
Datafile = open(filename,'r') line = Datafile.readline()

now this gives an extra empty line
print line

and what I expect that should be correct, remove CR+LF,
gives me one character too much removed
print line[,-2]

while this gives what I need ???
print line[,-1]

Is it correct that the 2 characters CR+LF are converted to 1 character ?
Is there a more automatic way to remove the EOL from the string ?
line = line.rstrip("\r\n") should take care of it. If you leave out the
parameter, it will strip out all whitespace at the end of the line,
which is what I do in most cases.
--
Michael Hoffman
Apr 27 '07 #2
>
line = line.rstrip("\r\n") should take care of it. If you leave out
the parameter, it will strip out all whitespace at the end of the
line, which is what I do in most cases.
thanks for the solution Michael,

cheers,
Stef
Apr 27 '07 #3
Jim
If you have a recent Python, see the documentation for open on the
library page for built-in functions.
http://docs.python.org/lib/built-in-funcs.html

Jim

Apr 27 '07 #4
On 27/04/2007 11:19 PM, Michael Hoffman wrote:
stef wrote:
>hello,

In the previous language I used,
when reading a line by readline, the EOL character was removed.
Very interesting; how did you distinguish between EOF and an empty line?
Did you need to call an isEOF() method before each read?
>>
Now I'm reading a text-file with CR+LF at the end of each line,
Datafile = open(filename,'r') line = Datafile.readline()

now this gives an extra empty line
print line

and what I expect that should be correct, remove CR+LF,
gives me one character too much removed
print line[,-2]
Stef, that would give you a syntax error. I presume that you meant to
type line[:-2]
>>
while this gives what I need ???
print line[,-1]

Is it correct that the 2 characters CR+LF are converted to 1 character ?
In text mode (the default), whatever is the line ending on your platform
is converted to a single "newline" '\n' which is the same as LF.

Using line[:-1] is NOT recommended, as the last line in your file may
not be terminated, and in that case you would lose the last data character.
>Is there a more automatic way to remove the EOL from the string ?

line = line.rstrip("\r\n") should take care of it. If you leave out the
parameter, it will strip out all whitespace at the end of the line,
which is what I do in most cases.
If you want *exactly* what is in the line, use line.rstrip('\n') -- this
will remove only the trailing newline (if it exists).

If you want to strip all trailing whitespace, use line.rstrip() as
Michael suggested.

Michael, note carefully that line.rstrip('\r\n') removes instances of
'\r' OR '\n' -- the arg is a set of characters to be removed, not a
suffix to be removed. In Stef's situation, it "works" only by accident.
Using that would not always give you the correct answer -- e.g. if your
(Windows) file had a line ending in CR CR LF [I've seen stranger].

HTH,
John
Apr 27 '07 #5
hi John,
>>In the previous language I used,
when reading a line by readline, the EOL character was removed.

Very interesting; how did you distinguish between EOF and an empty line?
Did you need to call an isEOF() method before each read?
Yes indeed, and I admit it needs some more coding ;-)
>
>>>
Now I'm reading a text-file with CR+LF at the end of each line,
Datafile = open(filename,'r') line = Datafile.readline()

now this gives an extra empty line
print line

and what I expect that should be correct, remove CR+LF,
gives me one character too much removed
print line[,-2]

Stef, that would give you a syntax error. I presume that you meant to
type line[:-2]
Yes, sorry.
>
>>>
while this gives what I need ???
print line[,-1]

Is it correct that the 2 characters CR+LF are converted to 1 character ?

In text mode (the default), whatever is the line ending on your platform
is converted to a single "newline" '\n' which is the same as LF.
Aha, that was the answer I was looking for.

<snip>

thanks for the splendid explanation John,

cheers,
Stef Mientki
Apr 28 '07 #6
John Machin wrote:
On 27/04/2007 11:19 PM, Michael Hoffman wrote:
>stef wrote:
>>hello,

In the previous language I used,
when reading a line by readline, the EOL character was removed.

Very interesting; how did you distinguish between EOF and an empty line?
Did you need to call an isEOF() method before each read?
>>>
Now I'm reading a text-file with CR+LF at the end of each line,
Datafile = open(filename,'r') line = Datafile.readline()

now this gives an extra empty line
print line

and what I expect that should be correct, remove CR+LF,
gives me one character too much removed
print line[,-2]

Stef, that would give you a syntax error. I presume that you meant to
type line[:-2]
>>>
while this gives what I need ???
print line[,-1]

Is it correct that the 2 characters CR+LF are converted to 1 character ?

In text mode (the default), whatever is the line ending on your platform
is converted to a single "newline" '\n' which is the same as LF.

Using line[:-1] is NOT recommended, as the last line in your file may
not be terminated, and in that case you would lose the last data character.
>>Is there a more automatic way to remove the EOL from the string ?

line = line.rstrip("\r\n") should take care of it. If you leave out
the parameter, it will strip out all whitespace at the end of the
line, which is what I do in most cases.

If you want *exactly* what is in the line, use line.rstrip('\n') -- this
will remove only the trailing newline (if it exists).

If you want to strip all trailing whitespace, use line.rstrip() as
Michael suggested.

Michael, note carefully that line.rstrip('\r\n') removes instances of
'\r' OR '\n' -- the arg is a set of characters to be removed, not a
suffix to be removed. In Stef's situation, it "works" only by accident.
Using that would not always give you the correct answer -- e.g. if your
(Windows) file had a line ending in CR CR LF [I've seen stranger].
I knew that about line.rstrip, but didn't consider the possibility of
\r\r\n, while still wanting the first \r. Yuck.

Honestly, I almost always use line.rstrip()--it is seldom that I care
about closing whitespace.
--
Michael Hoffman
Apr 28 '07 #7
On Apr 28, 7:25 pm, Michael Hoffman <cam.ac...@mh391.invalidwrote:
John Machin wrote:
On 27/04/2007 11:19 PM, Michael Hoffman wrote:
stef wrote:
hello,
>In the previous language I used,
when reading a line by readline, the EOL character was removed.
Very interesting; how did you distinguish between EOF and an empty line?
Did you need to call an isEOF() method before each read?
>Now I'm reading a text-file with CR+LF at the end of each line,
Datafile = open(filename,'r') line = Datafile.readline()
>now this gives an extra empty line
print line
>and what I expect that should be correct, remove CR+LF,
gives me one character too much removed
print line[,-2]
Stef, that would give you a syntax error. I presume that you meant to
type line[:-2]
>while this gives what I need ???
print line[,-1]
>Is it correct that the 2 characters CR+LF are converted to 1 character ?
In text mode (the default), whatever is the line ending on your platform
is converted to a single "newline" '\n' which is the same as LF.
Using line[:-1] is NOT recommended, as the last line in your file may
not be terminated, and in that case you would lose the last data character.
>Is there a more automatic way to remove the EOL from the string ?
line = line.rstrip("\r\n") should take care of it. If you leave out
the parameter, it will strip out all whitespace at the end of the
line, which is what I do in most cases.
If you want *exactly* what is in the line, use line.rstrip('\n') -- this
will remove only the trailing newline (if it exists).
If you want to strip all trailing whitespace, use line.rstrip() as
Michael suggested.
Michael, note carefully that line.rstrip('\r\n') removes instances of
'\r' OR '\n' -- the arg is a set of characters to be removed, not a
suffix to be removed. In Stef's situation, it "works" only by accident.
Using that would not always give you the correct answer -- e.g. if your
(Windows) file had a line ending in CR CR LF [I've seen stranger].

I knew that about line.rstrip, but didn't consider the possibility of
\r\r\n, while still wanting the first \r. Yuck.
It would be unusual to want that first \r -- a possibly more likely
scenario might be where your text file contains an extract from a
database, and you need to check that there are no unwanted (e.g.
unprintable) characters in the data (whether at the end of the line,
the middle, or the start).

In any case I think that you are missing the point that when reading a
normal text file on Windows with readline, while the line in the file
may be 'foo bar\r\n', what you get from readline is 'foo bar\n' -- so
in normal usage, the \r in your line.rstrip('\r\n') is pointless.
>
Honestly, I almost always use line.rstrip()--it is seldom that I care
about closing whitespace.
Honestly, I almost always split a line into fields and then for each
field, strip leading and trailing whitespace, and change runs of 1 or
more whitespace characters to a single space -- where "whitespace"
includes the pesky U+00A0 aka &nbsp; which doesn't qualify as
whitespace in a str instance.

Cheers,
John

Apr 28 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Philip Ronan | last post by:
OK, here's my 2p worth: === Q. Why am I getting the error message 'Headers already sent'? A. PHP produces this error message when you try to set a header for a web page after you have already...
4
by: Surya Kiran | last post by:
Hi all, I'm facing a wierd problem. I've a file, which is getting updated every now and then. and i'm having another program, which monitors the file. I've to read the file line by line, and in...
5
by: Mike Murray | last post by:
Hi I have the following issue. I have a character that is return by a SQL Server database "É" to be precise, the issue is that when I store character in a .net string variable my understanding...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
5
by: Durgesh Sharma | last post by:
Are there no genric Macros in c to represent Integers,Characters,...or other data types ? I want to pass that MACRO (representing an alpha numeric Character)to that strrchar() function,to get the...
9
by: Velvet | last post by:
I'm trying to convert some JavaScript to C# and don't know how to get the character code of a character in a string. in JavaScript it is as follows: for( i = 0; i < email.length; i++) { ...
36
by: Chuck Faranda | last post by:
I'm trying to debug my first C program (firmware for PIC MCU). The problem is getting serial data back from my device. My get commands have to be sent twice for the PIC to respond properly with...
1
by: prakash.appidi | last post by:
Hi I am getting the following error when I tried to run the code given below ORA-06550: line 1, column 29: PLS-00553: character set name is not recognized ORA-06550: line 0, column 0:...
3
by: Jeff | last post by:
I have had this function work perfectly in IE and am trying to get it to work in Firefox. I have seen plenty of questions and answers on the web for how to get and check the key pressed but nothing...
1
by: desivirus | last post by:
hi admin.. i followed your tip in "HOW TO LIST PROCESS ID IN WINDOWS" thread..and iam trying to compile this code in cygwin , $gcc -mno-cygwin process.c -o -L"psapi.lib" process.exe psapi.h...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.