Using Python I want to read a text file, search for a string and print all lines between this matching string and another one.
The textfile looks like the following:
Text=variables.Job_SalesDispatch.CaptionNew
Tab=0
TabAlign=0
}
}
}
[UserVariables]
User1=@StJid;IF(fields.Fieldtype="Artikel.Gerät" , STR$(fields.id,0,0) , @StJid)
[Parameters]
[@Parameters]
{
[Parameters]
{
LL.ProjectDescription=? (default)
LL.SortOrderID=
}
}
[PageLayouts]
[@PageLayouts]
{
[PageLayouts]
{
[PageLayout]
{
DisplayName=
Condition=Page() = 1
SourceTray=0
Now I want to print all "UserVariables", so only the lines between [UserVariables] and the next line starting with a square bracket. In this example this would be [Parameters].
What I have done so far is:
with open("path/testfile.lst", encoding="utf8", errors="ignore") as file:
for line in file:
uservars = re.findall('\b(\w*UserVariables\w*)\b', line)
print (uservars)
what gives me only [].
User1=@StJid;IF(fields.Fieldtype="Artikel.Gerät" , STR$(fields.id,0,0) , @StJid)in this example. But it is also possible to have more UserVariables likeUser2=@StJid;IF(fields.Fieldtype="Artikel.Referenzgerät" , STR$(fields.id,0,0) , @StJid).