[Tutor] File parsing
spawgi at gmail.com
spawgi at gmail.com
Thu Jun 16 21:56:34 CEST 2011
Hello Neha,
I think this script will do what you want and will also fix some of the
issues in your original code -
--------------------------------------------------------
1 #! /usr/bin/python
2
3 import curses.ascii
4 import string
5 import sys
6
7 with open(sys.argv[1],'r') as input:
8 for eachline in input:
9 eachline = eachline.strip('\n')
10 if curses.ascii.isalpha(eachline[0]):
11 first_char = ''
12 else:
13 first_char = eachline[0]
14 last_char = eachline[len(eachline)-1]
15 eachline = eachline.lstrip(first_char)
16 eachline = eachline.rstrip(last_char)
17 words = eachline.split(' ')
18 outline = first_char
19 for word in reversed(words):
20 outline = outline + str(word) + ' '
21 outline = outline.rstrip() + last_char + '\n'
22 print outline
-----------------------------------------------------------------------------------
It is much more explicit. It considers some special cases and gives the
output in the format desired by you.
Regards,
SWP
On Fri, Jun 17, 2011 at 12:21 AM, Neha P <mywrkid at yahoo.com> wrote:
> Thanks James
>
> I guess i have to use the same code for text in yellow... seems like ther's
> no other way...
>
> Regards,
> Neha
> ------------------------------
> *From:* James Reynolds <eire1130 at gmail.com>
> *To:* Neha P <mywrkid at yahoo.com>
> *Cc:* "tutor at python.org" <tutor at python.org>
> *Sent:* Thursday, June 16, 2011 2:43 PM
> *Subject:* Re: [Tutor] File parsing
>
> 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
>
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
http://spawgi.wordpress.com
We can do it and do it better.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110617/7127a3d3/attachment-0001.html>
More information about the Tutor
mailing list