[Tutor] File parsing

Lisi lisi.reisz at gmail.com
Thu Jun 16 21:48:03 CEST 2011


Thanks, Neha.  I am sending this back on list so that those who are more 
knowledgeable than I am can help you.  

On Thursday 16 June 2011 18:47:39 Neha P wrote:
> Hi Lisi,
> Below is the code and I have prefixed(-->) to the line code which i'm
> mainly concerned about and have commented using #
>
>
>
> for eachline in f_obj:
>
> # to eliminate the trailing "\n"    
> -->        eachline=eachline[ : -1]  
>      list_words=eachline.split(" ")
> # to add "\n" so that after line 1 is printed, line 2 should start on a new
> line""" -->    list_words[0]=list_words[0]+"\n"
>             list_words.reverse()
> # 'comma' helps in printing words on same
> # line, hence for last word have to append "\n" as done in above stmt
>          for every_word in list_words:
>                   print every_word,
>
>
> Thanks,
> Neha
>
>
>
>
> ________________________________
> From: Lisi <lisi.reisz at gmail.com>
> To: Neha P <mywrkid at yahoo.com>
> Sent: Thursday, June 16, 2011 1:19 PM
> Subject: Re: [Tutor] File parsing
>
> On Thursday 16 June 2011 18:03:55 Neha P 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
>
> Colours haven't transmitted (I use plain text).
>
> Could you identify the blocks of text by some other means?
>
> Thanks,
> Lisi


More information about the Tutor mailing list