简体   繁体   English

搜索和替换文本文件中的字符串

[英]Search and Replace a string in text file

I would like to search for a line in a text file which contains the string "SECTION=C-BEAM" and replace the first 13 characters in the "next line" by reading a pattern from first line (pattern highlighted in bold (see example below - read 1.558 from first line and replace it with 1.558/2 =0.779 in the second line). The number to read from first line is always in between the strings "H_" and "H_0".我想在包含字符串“SECTION=C-BEAM”的文本文件中搜索一行,并通过从第一行读取模式来替换“下一行”中的前 13 个字符(模式以粗体突出显示(参见示例)下面 - 从第一行读取 1.558 并在第二行用 1.558/2 =0.779 替换它。从第一行读取的数字总是在字符串“H_”和“H_0”之间。

Example Input:示例输入:

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

Output as follows:输出如下:

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

This is what I have tried so far.这是我到目前为止所尝试的。

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()

Basic python regex should do it :基本的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)

As you have mentioned that the content resides in a file, I tried to store some other random lines in a string other than the pattern you are looking for.正如您提到的内容驻留在文件中,我尝试将其他一些随机行存储在字符串中,而不是您要查找的模式。 Tested below piece of code and it works.在下面的一段代码中进行了测试,它可以工作。 I assume there is only one such occurrence in the file.If there are multiple occurrences in the file that can be done through a loop though.我假设文件中只有一个这样的事件。如果文件中有多个事件可以通过循环来完成。

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"  

Output as shown below:输出如下图:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM