473,394 Members | 1,852 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,394 software developers and data experts.

\r for newline in readlines function

I did more investigation into my previous problem
and what happens is that my text file has \r for representing
a new line instead
of a \n. is there a way to tell the readlines
function that the symbol for a newline is \r rather
than \n ? right now the readlines function reads
the\r as another item in the list and just puts
everything into one big list because it
doesn't find any new line characters.
thanks. i'm really stuck because
i can't read my data right now.

mark

Jul 18 '05 #1
3 11507
On Fri, 19 Sep 2003 12:37:42 -0400, in article
<ma**********************************@python.org >, Leeds, Mark wrote:
I did more investigation into my previous problem
and what happens is that my text file has \r for representing
a new line instead
of a \n.
If you open a file in text mode, you're probably getting text as the
implementation library or operating system view it.

Under MS-DOS and derivative system (e.g., Windows), text files are stored
with CRLF as the line delimiter but are returned to the program without the
CR. Under MacOS text files use CR as the delimiter, and under Unix, LF.
is there a way to tell the readlines
function that the symbol for a newline is \r rather
than \n ?


I think this is proper behavior, and the problem lies in the combination of
file format and the operating system under which you're executing. A
simple but inefficient solution is the write your own input function to
read the file one character at a time and handle characters like CR and LF
in a manner suitable to your requirements.
Jul 18 '05 #2
Leeds, Mark wrote:
I did more investigation into my previous problem
and what happens is that my text file has \r for representing
a new line instead
of a \n. is there a way to tell the readlines
function that the symbol for a newline is \r rather
than \n ? right now the readlines function reads
the\r as another item in the list and just puts
everything into one big list because it
doesn't find any new line characters.
thanks. i'm really stuck because
i can't read my data right now.

mark


Try opening the file with the "U" universal newlines flag:

f = file(filename, "rU")
print f.readlines()

I think this can cope with all three newline variants regardless of the
actual platform.

Peter
Jul 18 '05 #3
In article <ma**********************************@python.org >,
"Leeds, Mark" <ml****@mlp.com> wrote:
I did more investigation into my previous problem
and what happens is that my text file has \r for representing
a new line instead
of a \n. is there a way to tell the readlines
function that the symbol for a newline is \r rather
than \n ? right now the readlines function reads
the\r as another item in the list and just puts
everything into one big list because it
doesn't find any new line characters.
thanks. i'm really stuck because
i can't read my data right now.


Isn't this exactly what the new universal newline format is for?
<http://www.python.org/peps/pep-0278.html>

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #4

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

Similar topics

2
by: Pernell Williams | last post by:
Hi all: Thank you for your responses. I have a more specific question about "file.seek() and file.readline()" versus "file.seek() and file.xreadlines". When I have the following code:
9
by: wordsender | last post by:
Hey guys, I can't figure this one out, why is this simple script giving me problems? logfile=file(r'test.txt','w') logfile.write('datetime') test=logfile.readlines() When I run it I get...
34
by: Ross Reyes | last post by:
HI - Sorry for maybe a too simple a question but I googled and also checked my reference O'Reilly Learning Python book and I did not find a satisfactory answer. When I use readlines, what...
7
by: Alex Nordhus | last post by:
I am looking for a way to strip the blank line and the empty newline at the end of the text file. I can get the blank lines removed from the file but it always leaves the end line (which is blank)...
1
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
7
by: Nikhil | last post by:
Hi, I am reading a file with readlines method of the filepointer object returned by the open function. Along with reading the lines, I also need to know which line number of the file is read in...
0
by: Gary Herron | last post by:
Support Desk wrote: The problem has nothing to do with lists. The readlines() function returns each line *with* its newline. To strip it off, use line.strip() Gary Herron
5
by: zxo102 | last post by:
Hello All, I have a system. An instrument attched to 'com1' is wireless connected to many sensors at different locations. The instrument can forward the "commands" (from pyserial's write()) to...
5
by: alex21 | last post by:
I am trying to write a function for determining the data type of columns in a delimited file. However my function is not detecting a newline and exiting the loop after the end of the first line. ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.