1

It would be nice is someone can help me.

I have text files that looks like this :

#Zsdfgsdqfq
#A100,56
#Zjtdhsdfg
#Zadsdf
#B12,53
#Zdwfgs
#C10,00
#Zdsfgdfg
#D40,00

In the file #A, #B, #C and D# are numbers that I need to divide by 6.55957 This is the result that I want :

#A15,33
#Zjtdhsdfg
#Zadsdf
#B1,91
#Zdwfgs
#C1,52
#Zdsfgdfg
#D6,10

I've found this helpful discussion : Notepad ++ How to multiply numbers from mass replace?

So I used the Python script plugin in Notepad++

I can't figure how to manage decimal numbers and the comma used for decimal separator.

I've discovered I can delete all comas then divide the result by 100 and I will be able to search and replace to replace dots with comas in the resulting file.

Here is my script based on what I read on the linked discussion :

def multiply_number_in_context(match):
    return "{0}{1}".format(match.group(1), str(int(match.group(2))/655.957)) # thank you Wiktor Stribiżew to explain me how to get rid of this : ,''

editor.rereplace(r'(\#A)(\d+)', multiply_number_in_context)
editor.rereplace(r'(\#B)(\d+)', multiply_number_in_context)
editor.rereplace(r'(\#C)(\d+)', multiply_number_in_context)
editor.rereplace(r'(\#D)(\d+)', multiply_number_in_context)

Here is the result :

#Zsdfgsdqfq
#A15.33027317339399
#Zjtdhsdfg
#Zadsdf
#B1.910186185984752
#Zdwfgs
#C1.524490172374104
#Zdsfgdfg
#D6.097960689496415

It works but now I need to round the results with two decimals. #A should be 15.33 and #D should be 6.10

It seems the way is using the round(x, 2) thing but I can't figure ho to make this work.

I tried to modify my script with this sort of things this :

return "{0}{1}{2}".format(match.group(1), str(round(int(match.group(2))/655.957), '')), 2) 

or to modify the script so it rounds the numbers instead of multiplying but with no success I have syntax errors and such.

It would be kind if someone can improve my script so it rounds the result or give me another script that will round my results.

I don't want to learn programing I'm just searching for a solution.

Thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
anthonyb
  • 13
  • 3

1 Answers1

0

Use the following fixed script:

def multiply_number_in_context(match):
    return "{0:.2f}".format(float(match.group(0).replace(',', '.'))*100.0/655.957).replace(".",",")

editor.rereplace(r'(?<=#[A-Z])\d+(?:,\d+)?', multiply_number_in_context)

See the regex demo. Details:

  • (?<=#[A-Z]) - a positive lookbehind that matches a location immediately preceded with # and then any uppercase ASCII letter (change to [A-D] to only match A, B, C or D)
  • \d+ - 1+ digits
  • (?:,\d+)? - an optional substring that matches
    • , - a comma
    • \d+ - 1 or more digits.

Results:

enter image description here

Notes

  • match.group(0).replace(',', '.') replaces the comma with a dot to simplify calculations
  • float(...)*100.0/655.957 - the found number is cast to float, multiplied by 100 and divided by 655.957
  • "{0:.2f}".format(...) - formats the float rounded result
  • .replace(".",",") - reverts the decimal separator to ,.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563