Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Any algorithm to preserve whitespaces?

Reply
Thread Tools

Re: Any algorithm to preserve whitespaces?

 
 
Dave Angel
Guest
Posts: n/a
 
      01-23-2013
On 01/23/2013 04:20 AM, Santosh Kumar wrote:
> I am in a problem.
>
> words = line.split(' ')
>
> preserve whitespaces but the problem is it writes an additional line
> after every line.
>


Think about what you said. It might be clearer if you wrote:

"but the problem is it doesn't strip off the newline (which is whitespace)."

You might want to fix it by doing an rstrip(), as Peter said, or you
might want to check if the last character is \n, and delete it if so.

Or you might want to fix the other logic where you use the reconstituted
line, making sure it doesn't add an extra newline to a line that already
has one.

Best answer depends on whether there might be other whitespace at the
end of the line, and on whether you consider the newline part of the
last field on the line.

Chances are that Peter's response is the one you want, but I had to
point out that without a spec, we're really just guessing. For another
example, suppose that some of the words in the file are separated by
tabs. If so, perhaps you'd better rethink the whole split logic.




--
DaveA
 
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
Re: Any algorithm to preserve whitespaces? Santosh Kumar Python 0 01-23-2013 09:20 AM
Re: Any algorithm to preserve whitespaces? Mitya Sirenef Python 0 01-19-2013 07:04 PM
Re: Any algorithm to preserve whitespaces? Lie Ryan Python 0 01-19-2013 10:26 AM
Any algorithm to preserve whitespaces? Santosh Kumar Python 0 01-19-2013 10:13 AM
FileUtils - :preserve does not preserve mtime of directories on Windoze Grzegorz Chrupala Ruby 2 06-30-2003 07:38 AM



Advertisments