Menu

Find/replace problem where text to replace not the same as text to find

Help
Andie P
2016-06-22
2016-07-06
  • Andie P

    Andie P - 2016-06-22

    I have to develop a script that converts an XML file exported from a MySql database, to one that can be imported into another database. I am limited with the export, by MySql, and with the import by the structure of the larger database.

    A lot of it has already been solved, but I am stuck. I am new at PythonScript, I am sure it can be done, but I am having no luck. I have been through everything here, found topics that gave me ideas, but I still can't get this part of the script to work properly.

    This is what I am trying to do. My XML file has been fixed partially by editor.rereplace and editor.deleteLine etc. It looks like this, in part (after the script hs done the bits that work):

    <DEXFileUpload>

    <Sessions>
    
        <Session>
            <SessionId>22-01-2016 C</column>
            <CaseId>Craft Group</column>
            <SessionDate>2016-01-22</column>
            <ServiceTypeId>78</column>
            <TotalNumberOfUnidentifiedClients>0</column>
            <ClientId>003</column>
            <ParticipationCode>CLIENT</column>
            <ClientId>035</column>
            <ParticipationCode>CLIENT</column>
            <ClientId>067</column>
            <ParticipationCode>CLIENT</column>
            <TimeMinutes>180</column>
        </Session>
        <Session>
            <SessionId>12-02-2016</column>
    

    etc etc etc

    Now I have a lot of </column> tags, each needs to change to the closing tag that matches the respective opening tag. So for the <SessionId> lines I need </SessionId> at the end, but for the lines that begin <ClientId> I need </Column> to change to </ClientId>. I've tried a bunch of different methods. This is the last thing I tried, based on another topic here, but what it actually did was delete lines 4 to 13. I cut the contents of the function out, replaced them one line at a time, and now although all I did was cut/paste out and back, it does nothing at all....

    This is my function as it stands now:

    def endColumnReplace(startText, textToFind, newText):

    editor.gotoPos(0)
    editor.searchAnchor()
    last_found_position = editor.searchNext(0, startText)
    
    while last_found_position != -1:
        last_found_line = editor.lineFromPosition(last_found_position)
        editor.gotoLine(last_found_line)
    
        replace_position = editor.searchNext(0, textToFind)
        editor.setTargetStart(replace_position)
        editor.setTargetEnd(replace_position + 9)
        editor.replaceTarget(newText)
    
        editor.gotoLine(last_found_line + 1)
        editor.searchAnchor()
        last_found_position = editor.searchNext(0, startText)
    
    return
    

    and I call it like this:
    endColumnReplace('<SessionId>', '</column>', '</SessionId>')

    but what it does is nothing. I don't think there is a way to step through the script to see what it is doing and where it is going so I am at my wits end trying to figure out what I have done wrong. How can I make it work?Thank you :)

    Added: Sorry, I made a couple of silly errors when I was typing this last night. I have fixed them. I hope lol

     

    Last edit: Andie P 2016-06-23
  • Andie P

    Andie P - 2016-06-23

    It's OK I think, I am getting there. It does the first one correctly now, it just goes a bit stupid afterwards but that's progress lol; I am thinking its probably only a very minor error...

     
  • Andie P

    Andie P - 2016-06-23

    In case this can help someone else at some time, I got it working myself... here is the correct function:

    def endColumnReplace(startText, textToFind, newText):

    editor.gotoPos(0)                                           #goes to beginning of document
    editor.searchAnchor()                                       #sets the search anchor to the beginning of document
    last_found_position = editor.searchNext(0, startText)       #searches for the next occurrence of startText (<SessionId>)
    
    while last_found_position != -1:
        last_found_line = editor.lineFromPosition(last_found_position)  #retrieves the line number where startText was found
        editor.gotoLine(last_found_line)                        #and goes to that line
    
        editor.searchAnchor()                                   #sets the search anchor to that line
        replace_position = editor.searchNext(0, textToFind)     #searches for the text to be replaced
        editor.setTargetStart(replace_position)
        editor.setTargetEnd(replace_position + 9)
    
        editor.replaceTarget(newText)                           #replaces the unwanted text
    
        editor.gotoLine(last_found_line + 1)                    #moves to the next line and starts the search again
        editor.searchAnchor()
        last_found_position = editor.searchNext(0, startText)
    
    return
    
     
    • Justin Brewster

      Justin Brewster - 2016-07-06

      Andie:

      The python script has a console in which you can output too. Not sure if you knew that, but that's always a good place to start. Also anytime I run across a module I am not sure about, I run a help() command from the console on it.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.