I have a file that is in ASCII format, and I want to find a block of text surrounded by brackets, and get information from that file. The blocks look like this:
"material" "DmeMaterial"
{
"id" "elementid" "12af09eb-3a16-42a9-93eb-a9081a056d6d"
"name" "string" "BodyParts_diffuse"
"mtlName" "string" "models/characters/background/punk_01/BodyParts_diffuse"
}
I can load up the file and the data prints out fine in the console with this code:
filePath = "F:\SteamLibrary\SteamApps\common\SourceFilmmaker\content_custom\mod_dmx\material_test.dmx"
with open(filePath, "r+") as f:
data = f.read()
print data
f.close()
But I want to crawl through the text and find "material" "DmeMaterial" then grab the information in between the brackets (specifically mtlName and name.) It's a bunch of complicated steps in my mind and I'm hoping someone can help me out or guide me in the right path
This is one way to go about it:
filePath = 'F:\SteamLibrary\SteamApps\common\SourceFilmmaker\content_custom\mod_dmx\material_test.dmx'
data_list = []
with open(filePath, "r+") as f:
data = f.read()
marker, pos = '"material" "DmeMaterial"', 0
while data.find(marker) != -1: # Loops as long as the marker is present
pos = data.find(marker) # Finds first occurrence of marker
start = data.find('{', pos) # Finds first occurrence of { after marker
stop = data.find('}', pos) # Finds first occurrence of } after marker
data_list.append(data[start : stop+1].replace('\n', ''))
data = data[stop+1:]
data_list
will contain all the blocks.