mac text files & for line

H

Humberto

Greetings.

This is probably a v. basic question, but my apologies as I'm
relatively new w/ this.

But I am attempting to use <i>for line</i> to iterate through a text
file, but I am working on a Mac and am getting a single block of text.
I assume this is because of the Mac {CR} usage vs. line feed.

Is there a programmatic way to use for line to interpret the carriage
return character as a new line? Otherwise, what are the easiest ways
to be able to force a replacement of the {CR} character w/ the line
but receive an said:
file2.txt command on my Mac.

Any help would be greatly appreciated and thanks!
 
M

MRAB

Humberto said:
Greetings.

This is probably a v. basic question, but my apologies as I'm
relatively new w/ this.

But I am attempting to use <i>for line</i> to iterate through a text
file, but I am working on a Mac and am getting a single block of text.
I assume this is because of the Mac {CR} usage vs. line feed.

Is there a programmatic way to use for line to interpret the carriage
return character as a new line? Otherwise, what are the easiest ways
to be able to force a replacement of the {CR} character w/ the line


Any help would be greatly appreciated and thanks!

Open the file with mode 'U' for universal newline support ('\n', '\r' or
'\r\n').
 
L

Lie Ryan

Humberto said:
Greetings.

This is probably a v. basic question, but my apologies as I'm
relatively new w/ this.

But I am attempting to use <i>for line</i> to iterate through a text
file, but I am working on a Mac and am getting a single block of text.
I assume this is because of the Mac {CR} usage vs. line feed.

Is there a programmatic way to use for line to interpret the carriage
return character as a new line? Otherwise, what are the easiest ways
to be able to force a replacement of the {CR} character w/ the line


Any help would be greatly appreciated and thanks!

<psychic_mode>
I guess this is how you write your code:
f = open('myfile.txt', 'r').read()
for line in f:
print line
# stream of characters...

if that's the case, change the code into:
f = open('myfile.txt', 'r')
for line in f:
print line

If you .read() the file yourself, you'll get a single string of the
whole file content; '\n' (of whatever real type) inside a string is not
used as delimiter for a for-loop.
</psychic_mode>
 
H

Humberto

Open the file with mode 'U' for universal newline support ('\n', '\r' or
'\r\n').

Precisely my problem. Thanks so much. I'd overlooked the references to
U in the entry for the open function.

To the other fellow, I think the question was reasonably specific in
the second paragraph...I sense the helpful response bore that out. I
certainly acknowledge my mistake in having overlooked the reference in
the documentation. So my apologies for any inconvenience and thanks
again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top