How to set/query TITLE parameter when creating a GXF file using Python API?
EdwardBiegert
Posts: 4
I created a working python script that creates a GXF file from scratch.
What function is used to set the TITLE string for the GXF file? Or to query it, for that matter?
#TITLE
#POINTS
108
#ROWS
107
#SENSE
1
...
What function is used to set the TITLE string for the GXF file? Or to query it, for that matter?
#TITLE
#POINTS
108
#ROWS
107
#SENSE
1
...
0
Best Answer
-
Hi Edward,
I do not believe there are GX API methods for doing this. However since the GXF format is a simple text format this should be simple to achieve using built in Python methods. Here is some (untested) example code that could be used to make it work:GXF_TITLE_PREFIX = '#TITLE' def get_gxf_title(gxf_file): with open(gxf_file) as f: lines = f.readlines() title_index = next((l[len(GXF_TITLE_PREFIX):].strip() for i, l in enumerate(lines) if l.upper().startswith(GXF_TITLE_PREFIX)), None) return '' if title_index is None or title_index >= len(lines - 1) else lines[title_index + 1].strip().strip('"') def set_gxf_title(gxf_file, title): with open(gxf_file) as f: lines = f.readlines() title_index = next((l[len(GXF_TITLE_PREFIX):].strip() for i, l in enumerate(lines) if l.upper().startswith(GXF_TITLE_PREFIX)), None) if title_index is not None: if title_index < len(lines): lines.pop(title_index) if title_index < len(lines): lines.pop(title_index) lines.insert(0, f'{GXF_TITLE_PREFIX}\n') lines.insert(1, f'{title}\n') with open(gxf_file, 'w') as f: f.writelines(lines)
5
This discussion has been closed.