Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Solved] Python replace line in buffer

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
taupist



Joined: Sat Jan 14, 2006 6:24 pm
Posts: 20
Location: Idaho, USA

PostPosted: Sun Nov 24, 2013 5:29 am    Post subject: [Solved] Python replace line in buffer Reply with quote

I'm trying to replace a line of text in a buffer, can anyone see what I'm doing wrong?

I have a textview with a buffer "texbuff", I load a line of text from "texbuff" into a textinput called "ocredit". When I correct the text in "ocredit" I want to replace the corresponding line in "texbuff" and the textview.

Thx

Code:

     def ocredit(self, old_line):
         m = self.counter
         self.ocredit = self.builder.get_object("ocredit")
         self.ocredit.modify_font(pango.FontDescription("Monospace 11"))
         buff = self.texbuff
         text = self.getbuffer(buff)
         text = text.splitlines()
         #FIXME
         o = StringIO()
         #FIXME Try: here!
         try:
             for line in text:
                 # Search for old_line
                 if str(old_line) in line:
                     new_line = self.ocredit.get_text() + '\n'
                     line.replace(old_line, new_line)
                     o.write(new_line)
          #FIXME Except: pass here!
         except:
             pass
         try:
             #FIXME Text iters here?
             #print(reversed(range(len(text))))
             if m <= range(len(text)):
                 #FIXME old_line is here
                 old_line = text[m]
                 self.ocredit.set_text(text[m])
                 self.counter += 1
         except:
             self.builder.get_object("hbox4").hide()
             return


My function now looks like this.

Code:
    def ocredit(self, old_line):
        m = self.counter
        self.ocredit = self.builder.get_object("ocredit")
        self.ocredit.modify_font(pango.FontDescription("Monospace 11"))
        text = self.getbuffer(self.texbuff)
        text = text.splitlines()
        if self.old_line != '':
            try:
                new_line = self.ocredit.get_text()
                if self.old_line != new_line:
                    text = [line.replace(self.old_line,
                            new_line) for line in text]
                    n = 0
                    newtext = []
                    for line in text:
                        if n <= range(len(text)):
                            line = text[n] + '\n'
                        newtext.append(line)
                        n = n + 1
                newtext = '%s' % ''.join(map(str, newtext))
                self.texbuff.set_text(newtext)
            except:
                pass
        try:
            #FIXME Text iters here?
            #print(reversed(range(len(text))))
            if m <= range(len(text)):
                self.old_line = text[m]
                #print(old_line)
                self.ocredit.set_text(text[m])
                self.counter += 1
        except:
            self.builder.get_object("hbox4").hide()
            return


Last edited by taupist on Tue Nov 26, 2013 4:41 am; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast