Python: Append new line of string in specific line in the hierarchical structured text file
Question:
For example the content in text file is like this:
Component_X
Value1
Value1.1
Value1.2
#Here is where i want to append the new string#
Value1.3
Value2
Value2.1
Value2.2
Value2.3
Component_Y
Value1
Value1.1
Value1.2
Value1.3
Value2
Value2.1
Value2.2
Value2.3
Component_X
Value1
Value1.1
Value1.2
#Here is where i want to append the new string#
Value1.3
Value2
Value2.1
Value2.2
Value2.3
My question is, how can I append a new string at the specific component such as I want to append new string in the Value1.2 of Value1 of the Component_X by using Python? Hopefully anybody can help this out.
My expected output after write the new string into the text file is:
Component_X
Value1
Value1.1
Value1.2
true
Value1.3
Value2
Value2.1
Value2.2
Value2.3
Component_Y
Value1
Value1.1
Value1.2
Value1.3
Value2
Value2.1
Value2.2
Value2.3
Component_X
Value1
Value1.1
Value1.2
true
Value1.3
Value2
Value2.1
Value2.2
Value2.3
Answers:
You need to read the file one line at a time. You’ll need to build the content in a list. Search for the value that you want to insert after. Do the insert then rewrite the file.
def insert_after(filename, component, after_this, value):
in_component = False
with open(filename, 'r+') as text:
out_lines = []
for line in text.readlines():
if not line[0].isspace():
in_component = line.startswith(component)
out_lines.append(line)
if in_component and line.strip() == after_this:
pad = ' ' * (len(line) - len(line.lstrip()))
out_lines.append(f"{pad}{value}n")
text.seek(0)
text.writelines(out_lines)
insert_after('foo.txt', 'Component_X', 'Value1.2', 'true')
Basically the idea is to check last most nearest component if X or not, last element value is 1.2 or not.
Here, I am text lines as list, first find the all position of Component and keep it as dict, after that run another loop and check conditions 1.2 in lines[i-1]
and X in pos[[l for l in list(pos.keys())if l<i-1][-1]]]
Code:
def appendTxt(Alpha, Num, Add):
pos = {i:v for i,v in enumerate(lines) if 'Component'in v}
[lines.insert(i,Add) for i in range(1, len(lines)) if Num in lines[i-1] and Alpha in pos[[l for l in list(pos.keys())if l<i-1][-1]]]
return lines
lines = list(open('./t1.txt', 'r'))
appendTxt('X', '1.2', 'true')
Output:
['Component_Xn',
' Value1n',
' Value1.1n',
'n',
' Value1.2n',
'true',
'n',
' Value1.3n',
'n',
' Value2n',
' Value2.1n',
'n',
' Value2.2n',
'n',
' Value2.3n',
'n',
'Component_Yn',
' Value1n',
' Value1.1n',
'n',
' Value1.2n',
'n',
' Value1.3n',
'n',
' Value2n',
' Value2.1n',
'n',
' Value2.2n',
'n',
' Value2.3n',
'n',
'Component_Xn',
' Value1n',
' Value1.1n',
'n',
' Value1.2n',
'true',
'n',
' Value1.3n',
'n',
' Value2n',
' Value2.1n',
'n',
' Value2.2n',
'n',
' Value2.3']
For example the content in text file is like this:
Component_X
Value1
Value1.1
Value1.2
#Here is where i want to append the new string#
Value1.3
Value2
Value2.1
Value2.2
Value2.3
Component_Y
Value1
Value1.1
Value1.2
Value1.3
Value2
Value2.1
Value2.2
Value2.3
Component_X
Value1
Value1.1
Value1.2
#Here is where i want to append the new string#
Value1.3
Value2
Value2.1
Value2.2
Value2.3
My question is, how can I append a new string at the specific component such as I want to append new string in the Value1.2 of Value1 of the Component_X by using Python? Hopefully anybody can help this out.
My expected output after write the new string into the text file is:
Component_X
Value1
Value1.1
Value1.2
true
Value1.3
Value2
Value2.1
Value2.2
Value2.3
Component_Y
Value1
Value1.1
Value1.2
Value1.3
Value2
Value2.1
Value2.2
Value2.3
Component_X
Value1
Value1.1
Value1.2
true
Value1.3
Value2
Value2.1
Value2.2
Value2.3
You need to read the file one line at a time. You’ll need to build the content in a list. Search for the value that you want to insert after. Do the insert then rewrite the file.
def insert_after(filename, component, after_this, value):
in_component = False
with open(filename, 'r+') as text:
out_lines = []
for line in text.readlines():
if not line[0].isspace():
in_component = line.startswith(component)
out_lines.append(line)
if in_component and line.strip() == after_this:
pad = ' ' * (len(line) - len(line.lstrip()))
out_lines.append(f"{pad}{value}n")
text.seek(0)
text.writelines(out_lines)
insert_after('foo.txt', 'Component_X', 'Value1.2', 'true')
Basically the idea is to check last most nearest component if X or not, last element value is 1.2 or not.
Here, I am text lines as list, first find the all position of Component and keep it as dict, after that run another loop and check conditions 1.2 in lines[i-1]
and X in pos[[l for l in list(pos.keys())if l<i-1][-1]]]
Code:
def appendTxt(Alpha, Num, Add):
pos = {i:v for i,v in enumerate(lines) if 'Component'in v}
[lines.insert(i,Add) for i in range(1, len(lines)) if Num in lines[i-1] and Alpha in pos[[l for l in list(pos.keys())if l<i-1][-1]]]
return lines
lines = list(open('./t1.txt', 'r'))
appendTxt('X', '1.2', 'true')
Output:
['Component_Xn',
' Value1n',
' Value1.1n',
'n',
' Value1.2n',
'true',
'n',
' Value1.3n',
'n',
' Value2n',
' Value2.1n',
'n',
' Value2.2n',
'n',
' Value2.3n',
'n',
'Component_Yn',
' Value1n',
' Value1.1n',
'n',
' Value1.2n',
'n',
' Value1.3n',
'n',
' Value2n',
' Value2.1n',
'n',
' Value2.2n',
'n',
' Value2.3n',
'n',
'Component_Xn',
' Value1n',
' Value1.1n',
'n',
' Value1.2n',
'true',
'n',
' Value1.3n',
'n',
' Value2n',
' Value2.1n',
'n',
' Value2.2n',
'n',
' Value2.3']