[Tutor] File parsing
James Reynolds
eire1130 at gmail.com
Thu Jun 16 20:43:58 CEST 2011
use split on the list to split it up. search each element for something
like:
if '"' == element[:-1]:
if that evaluation is True, I would remove the quote mark from the word on
the right side, and place a new one on the left side using something like
'"' + element.
I would do the same thing for the other side in the same for loop, instead
the evaluation would be:
if '"' == element[:1]:
On Thu, Jun 16, 2011 at 1:03 PM, Neha P <mywrkid at yahoo.com> wrote:
> Hi all,
> I know below query may sound silly, but can somebody suggest any better way
> of doing this:
> It would be helpful.
>
> I need to read a file line by line and print each line starting from the
> last word first:
>
> C:\Python26>type file_reversing_program.txt
> import sys
> import string
>
> f_obj=open(sys.argv[1],"r")
>
> for eachline in f_obj:
> eachline=eachline[ :-1] # to eliminate the trailing "\n"
> list_words=eachline.split(" ")
> list_words[0]=list_words[0]+"\n" # to add "\n" so that after line 1 is
> printed, line 2 should start on a new line
> list_words.reverse()
> for every_word in list_words:
> print every_word, # 'comma' helps in printing words on same
> line,hence for last word we append "\n"
>
> f_obj.close()
>
> C:\Python26>type input_file.txt
> "Hi ther, how are you?"
> I are doing fine, thank you.
>
> C:\Python26>file_reversing_program.py input_file.txt
> you?" are how ther, "Hi
> you. thank fine, doing are I
>
> Is there a better way of doing the above program, mainly the text
> highlighted in yellow,
> Also if that is settled can there be a logic for getting the ouput more
> properly formatted (for text in blue) ,
> say giving an output like :
> "you? are how ther, Hi"
> you. thank fine, doing are I
>
>
> Thanks,
> Neha
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110616/4a23ae4b/attachment.html>
More information about the Tutor
mailing list