The task is as follows. The file is read line by line and as soon as the numbers come across in a pattern, they are multiplied and written into place. Simply put, I need to multiply all the numbers from the pattern by a constant. for example, the lines in the file:

values( " 2.326, 2.358, 2.418, 2.481, 2.601, 2.807",\ " 2.348, 2.379, 2.439, 2.503, 2.622, 2.828"); 

It is necessary to multiply all the numbers in the file by "k"

 file = open("txt.txt","r") for line in file: fix_line = re.sub(r'(\d+\.\d+)', '???*k', line) 

Tried to do through re.sub to replace in line, but stopped on how to arrange a replacement. How to capture each number and replace it with a new number, differing by the coefficient "k". Maybe there is a simpler approach to replacing numbers?

  • Do you have arbitrary strings? Or do they have the view you submitted? - hedgehogues
  • In this situation, I greatly facilitated the task. In more detail it sounds that in a text file with data to find the template that described above and multiply all the numbers in this template by the coefficient "k". The number of lines with numbers in the pattern may vary. That is, the file with tables of numbers in the values ​​(); field. - BorisN
  • Is the pattern indicated in the regular schedule? - hedgehogues
  • Yes, if I understood you correctly that the numbers are written in the file through a dot. I am looking first for the values ​​() ;, template that contains numbers, and then the numbers in the template to fix them with "number * k". - BorisN
  • @BorisN, what does the backslash in the template string mean? Are the numbers in the pattern on one line? Can you give several examples of lines from your file with templates in exactly the same format as in the file (without additional line breaks, etc.)? - MaxU

1 answer 1

To solve this problem, you need to iterate over all the found numbers, alternately retrieve them and replace them with a modified number.

It should be borne in mind that after modifying the number, the string corresponding to it may differ in length. So, you need to accumulate a total change in the "numbers-lines" and take this into account when inserting a line.

 import re k = 1.2 # Коэффициент file = open("txt.txt") sum_delta = 0 # Суммарное количество изменений длин строк for line in file: new_line = line for index in re.finditer(r'(\d+\.\d+)', line): interval = index.span() old_value_str = line[interval[0]:interval[1]] new_value_str = str(k * float(old_value_str)) # Модификация строки. Используем преффикс и суффикс от старой строки и новую "число-строку" new_line = new_line[:interval[0] + sum_delta] + \ new_value_str + \ new_line[interval[1] + sum_delta:] sum_delta += len(new_value_str) - len(old_value_str) print(new_line) 
  • Thanks for the idea, it worked, except that the float numbers were wild, which broke the algorithm and we got the following table values ​​("2.7912, 2.8296, 2.9016, 2.9772, 3.1212, 3.3684", \ "2.348,2.81759999999997.379,2.854848 .439,2.9268.503,3.0036.622,3.1464.828 "3.39359999999999" 2.373, 2.405, 2.465, 2.528.2.8476000000000004.647.2.8859999999999997.854 "2.957999999999973.03363.17999999999999.4.488 for a single reference (18) "); - BorisN
  • replaced new_value_str = str (k * float (old_value_str)) with new_value_str = str (format (round (k * float (old_value_str), 3), '. 3f')) and it all worked - BorisN