Поиск и замена строки в текстовом файле - PullRequest
0 голосов
/ 27 января 2020

Я хотел бы найти строку в текстовом файле, который содержит строку «SECTION = C -BEAM», и заменить первые 13 символов в «следующей строке», читая шаблон из первой строки (шаблон выделен жирным шрифтом (см. пример ниже - прочитайте 1.558 из первой строки и замените его на 1.558 / 2 = 0.779 во второй строке.) Число для чтения из первой строки всегда находится между строками "H_" и "H_0".

Пример ввода:

SECTION, ELSET=DIORH_1_558H_0_76W_241_1, SECTION=C-BEAM, MAT=XYZ;
0., 1,  2,  3,  4,  5

Вывод следующим образом:

SECTION, ELSET=DIORH_1_558H_0_76W_241_1, SECTION=C-BEAM, MAT=XYZ;
0.779,  1,  2,  3,  4,  5

Это то, что я пробовал до сих пор.

file_in = open(test_input, 'rb')
file_out = open(test_output, 'wb')
lines = file_in.readlines()
print ("Total no. of lines to process: ", len(lines))
for i in range(len(lines)):
    if lines.startswith("SECTION") and "SECTION=C-BEAM" in lines:
    start_index = lines.find("H_")+1
    end_index = lines.find("H_0")
    x = lines[start_index:end_index]/2.0
    print (x)
    lines[i+1]= lines[i+1].replace("          0.",x)+lines[i+1][13:]
file_out.write(lines[i])
file_in.close()
file_out.close()

Ответы [ 2 ]

0 голосов
/ 27 января 2020

Поскольку вы упомянули, что содержимое находится в файле, я попытался сохранить некоторые другие случайные строки в строке, отличной от искомого шаблона. Протестирован ниже кусок кода и все работает. Я предполагаю, что в файле есть только одно такое вхождение. Если в файле есть несколько вхождений, которые можно выполнить с помощью al oop.

import re

st = '''These are some different lines - you need not worry about.
SECTION, ELSET=DIORH_1_558H_0_76W_241_1, SECTION=C-BEAM, MAT=XYZ;
0., 1,  2,  3,  4,  5
These are more different lines - you need not worry about.
0.,2 numbers'''

num = str(float(re.findall('.*H_(.+)H_0.*SECTION=C-BEAM.*\n.*',st)[0].replace("_","."))/2)
print (re.sub(r'(.*SECTION=C-BEAM.*\n)(0\.)(,.*)',r'\g<1>'+num+r'\g<3>',st))

# re.findall('.*H_(.*)H_0.*SECTION=C-BEAM.*\n.*',st) --> Returns ['1_558']. Extract 1_558 by indexing it -[0] 
# Then replace "_" with "." Convert to a float, divide by 2 and then convert the result to string again
# .* means 0 or more non-newline characters,.+ means 1 or more non-newline characters "\n" stands for new line. 
# (.+) means characters inside the bracket from the overall pattern will be extracted
# Second line of the code: I replaced the desired number("0.") for the matching patternin the second line. 
# Divided the pattern in to 3 groups: 1) Before the pattern  "0." 2) The pattern "0." itself 3) After the pattern "0.". 
# Replaced the pattern "0." with "group 1 + num + group 2"  

Вывод, как показано ниже:

enter image description here

0 голосов
/ 27 января 2020

Basi c python регулярное выражение должно сделать это:

my_text = """SECTION, ELSET=DIORH_1_558H_0_76W_241_1, SECTION=C-BEAM, MAT=XYZ;\n0., 1,  2,  3,  4,  5"""

# This find the index of the first occurence of your regex in my_text
index = my_text.find('SECTION=C-BEAM')

# You select everything before the first occurence of your regex 
# and count the number of lines (\n is the escape line character)
nb_line = my_text[:index].count('\n')

# Now you wand to find the index of the beginning of the n + 1 line. 
# You can do this thanks to finditer function
# This creates the list of index of a specified regex, 
# you select the n + 1 (here it is nb_line because python indexing starts at 0)
index = [m.start() for m in re.finditer(r"\n",my_text)][nb_line]

# the you re build the wanted string with :
# the beginning of your string until the n + 1 line,
# the text you want (0.779) 
# the text after the substring you removed (you need to know the length of the string you want to remove here 2

string_to_remove = "0."
my_text = my_text[:index+1] + '0.779' + my_text[index + 1 + len(string_to_remove):]

print(my_text)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...