Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Convert VBA to Python
Please could someone convert the code below for me?
I need to convert VBA to Python


Public Function Clean_Text(main_text As String, _
                       cut_1 As String, _
                       cut_2 As String)
    Application.ScreenUpdating = False
    Dim x As String
    Dim begin As String
    Dim end As String
    x = main_text 
    Do While InStr(1, x, cut_1) > 0
        begin = InStr(1, x, cut_1)
        end = InStr(1, x, cut_2)
        x = Replace(x, Mid(x, begin, end - begin + Len(cut_2)), "")
    Clean_Text = x
End Function
What have you tried? We're not big on writing code for people here, but we would be happy to help you fix your code when you run into problems. When you do run into problems, please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Hello, unfortunately I don't know python, I only know vba, I'm looking for someone who knows vba and python and who can help me with this code, as it is a small and simple code I thought I would find someone
I was going to suggest the Jobs section of the forum, but took a second look at this and it should be rather trivial. It has been a long time since I did any VBA, though. Can you tell me what the function is supposed to do?

I see a lot of problems in the VBA code so that's why I want to start with the description. It looks like you are trying to remove a particular character sequence from the string, but the coding looks like it works only in very specific situations. For example, what if the cut_2 string is found before the cut_1 string? What if you still find cut_1 in the string but there are no more cut_2's? Looks like it will error out in both situations.

It looks like you want to remove all text from the beginning of the match to cut_1 to the end of cut_2, no matter how many times these two string snips are found, but it is unPythonic to guess. So, if you can explain what the function is supposed to do I am willing to take a whack at it.
buran likes this post
Thanks for answering =D

First I want to say that this function will handle text generated by a system, so the two situations you mentioned will never happen..
1) if the cut_2 string is found before the cut_1 string? Never happen
2) if you still find cut_1 in the string but there are no more cut_2's? Never happen

Now talking about what the function should do:
- she should remove pieces of text from a larger text
- the pieces to be removed will always be delimited by the same sequence at the beginning and end
- the text that will be between the delimiters is variable, we dont know the size or the information
- for example: use text "#123" to delimit the beginning and text "%456" to delimit the end
- an example of text would be: #123 hello %456 im eating #123@@%456 #123%456 now #123 bye%456
- the function must remove the sequences: #123 hello %456 / #123@@%456 / #123%456 / #123 bye%456
- and the end result should be: im eating now

I hope I explained it clearly
If you can help me I will be very grateful
ok, here's the minimum:
def clean_text(main_text, cut_1, cut_2) :
    while main_text.find(cut_1) != -1 :
        begin = main_text.find(cut_1)
        end = main_text.find(cut_2)
        main_text = main_text[0:begin]+main_text[end+len(cut_2):]
    return main_text

snippsat likes this post
Worked perfectly!!
Thank you very much!!! =D

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)