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

readling newlines

Hi, I'm trying to detect the newlines method of a file opened.
I'm using the 'U' mode as parameter when opening a file, but for every file opened the result is always "\r\n", even if the file has
been saved with the "\n" newline method.
Is there i'm missing?

I'm on Windows with python 2.3.3
Jul 18 '05 #1
4 1745
|Thus Spake Alessandro Crugnola *sephiroth* On the now historical date of
Sat, 10 Jan 2004 14:25:44 +0000|
Hi, I'm trying to detect the newlines method of a file opened. I'm using
the 'U' mode as parameter when opening a file, but for every file opened
the result is always "\r\n", even if the file has been saved with the
"\n" newline method. Is there i'm missing?

I'm on Windows with python 2.3.3


Not quite sure what "U" mode is, but try adding the "b" mode when opening
the file. Windows makes a distinction between binary and text files.
Python defaults to text mode, and since newlines for windows text is \r\n,
that's what Python puts there.

HTH

Sam Walters.

--
Never forget the halloween documents.
http://www.opensource.org/halloween/
""" Where will Microsoft try to drag you today?
Do you really want to go there?"""

Jul 18 '05 #2
Alessandro Crugnola *sephiroth* wrote:
Hi, I'm trying to detect the newlines method of a file opened.
I'm using the 'U' mode as parameter when opening a file, but for every
file opened the result is always "\r\n", even if the file has been saved
with the "\n" newline method. Is there i'm missing?

I'm on Windows with python 2.3.3


Here's what I get (on Linux):
for nl in ["\n", "\r\n", "\n\r", "\r"]: .... dst = file("tmp.txt", "wb")
.... dst.write("one%stwo" % nl)
.... dst.close()
.... print repr(file("tmp.txt", "U").read())
....
'one\ntwo'
'one\ntwo'
'one\n\ntwo'
'one\ntwo'


From the documentation of file():

If Python is built without universal newline support mode 'U' is the same as
normal text mode.

So maybe you have a version without universal newline support?

Peter

Jul 18 '05 #3
Samuel Walters wrote in message ...

Not quite sure what "U" mode is, but try adding the "b" mode when opening


FYI, 'U' is universal newline mode, which will quietly convert the newline
convention of a file into '\n'. I think it has some rudamentary ability to
handle mixed-convention newline ugliness.

See the documentation on file() in the library reference, and there's a PEP
on the feature, too.

--
Francis Avila

Jul 18 '05 #4

"Alessandro Crugnola *sephiroth*" <al********@sephiroth.it> wrote in message
news:IX********************@news4.tin.it...
Hi, I'm trying to detect the newlines method of a file opened.
I'm using the 'U' mode as parameter when opening a file, but for every file opened the result is always "\r\n", even if the file has been saved with the "\n" newline method.
Is there i'm missing?

I'm on Windows with python 2.3.3
Regardless of what you tell it, Python will always write a text
file with the operating system's line separators. In Windows,
that's \r\n. Universal newline mode only operates on input,
and will translate *all* common system's line separators into
/n.

In other words, if you write a file in text mode using /n, and
then read it back in binary mode, you'll see /r/n for the line
separators. Universal newline support does not change this
because it does not change what you see in binary mode.

John Roth

Jul 18 '05 #5

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

Similar topics

4
by: Christopher Finke | last post by:
I have a site wherein the clients can update the individual pages by editing the text of that page in a <textarea>. Then, when the page is displayed, all of the newlines (\n) are converted to HTML...
4
by: Grant Edwards | last post by:
I'm using xml.sax to parse the "datebook" xml file generated by QTopiaDesktop. When I look at the xml file, some of the attribute strings have newlines in them (as they are supposed to). ...
6
by: dbee | last post by:
So I can't seem to urlencode a file with newlines ... it just gives me a series of T_STRING unexpected parse errors... cat job_description | while read file ; do php -r "echo...
1
by: dbee | last post by:
Hi, So I'm having a problem with disappearing newlines. I import the newlines from a file into my shell script fine. But then I process the text and the url_encode comes out the other end with...
0
by: skip | last post by:
*argh!* I hate XML! There, now that that's off my chest... I am trying to save Python code as attributes of an XML tag with xml.dom.minidom machinery. The code, predicatbly enough, contains...
2
by: Edward K. Ream | last post by:
Hello all, I recently ran across a situation in which sax.saxutils.quoteattr did not work as I expected. I am writing Leo outlines as opml files http://en.wikipedia.org/wiki/OPML which forces...
1
by: HopfZ | last post by:
Not a question. I tested how two kinds of newlines (\n and \r\n) interact with three browsers: Fx (Firefox 2), Op (Opera 9), Ie (IE 7) (all three on Windows XP). Result: The string...
5
by: lister | last post by:
Hi, I am trying to output a newline, but this doesn't work: echo "Line1\nLine2"; Before anyone says, I know HTML doesn't recognise newlines. I don't need newlines in the rendered HTML, I...
4
by: lihao0129 | last post by:
Hi, folks: I recently went through a strange problem with my Javascript code, say: I have a string variable which are from a 'textarea' element and I want to remove the trailing newlines inside...
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.