Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > mac text files & for line

Reply
Thread Tools

mac text files & for line

 
 
Humberto
Guest
Posts: n/a
 
      06-16-2009
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
feed? I've attempted the method using <i>tr</i>, but receive an
illegal byte sequence error when running the tr '\r' '\n' < file1.txt
> file2.txt command on my Mac.


Any help would be greatly appreciated and thanks!
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      06-16-2009
Humberto wrote:
> 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
> feed? I've attempted the method using <i>tr</i>, but receive an
> illegal byte sequence error when running the tr '\r' '\n' < file1.txt
>> file2.txt command on my Mac.

>
> Any help would be greatly appreciated and thanks!


Open the file with mode 'U' for universal newline support ('\n', '\r' or
'\r\n').
 
Reply With Quote
 
 
 
 
Lie Ryan
Guest
Posts: n/a
 
      06-16-2009
Humberto wrote:
> 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
> feed? I've attempted the method using <i>tr</i>, but receive an
> illegal byte sequence error when running the tr '\r' '\n' < file1.txt
>> file2.txt command on my Mac.

>
> 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>
 
Reply With Quote
 
Humberto
Guest
Posts: n/a
 
      06-16-2009
On Jun 16, 1:39*pm, MRAB <(E-Mail Removed)> wrote:
> Humberto wrote:
> > 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
> > feed? I've attempted the method using <i>tr</i>, but receive an
> > illegal byte sequence error when running the tr '\r' '\n' < file1.txt
> >> file2.txt command on my Mac.

>
> > Any help would be greatly appreciated and thanks!

>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
Mouse Location to Text line or Text line to location kuna.subramaniyam@gmail.com Javascript 2 04-14-2008 01:46 PM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM
Beginner: read $array with line breaks line by line Marek Stepanek Perl Misc 12 09-02-2006 10:27 AM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM



Advertisments