Skip to content

Instantly share code, notes, and snippets.

@notyal
Last active November 17, 2024 05:32
Show Gist options
  • Save notyal/41af8ea1328ea62f1ca62eaebeb488c0 to your computer and use it in GitHub Desktop.
Save notyal/41af8ea1328ea62f1ca62eaebeb488c0 to your computer and use it in GitHub Desktop.
Modify SimosTools logfile headers so MegaLogViewer can open multiple logs at once
# Modify SimosTools logfile headers for use with MegaLogViewer
from glob import iglob
def modify_header(file):
with open(file, 'r+') as f:
# read header of file
header = f.readline()
cols = str.split(header, ',')
# check header
if not cols[-1].startswith("SimosTools"):
print("ERROR: Invalid Header: Missing 'SimosTools' in last column.")
f.close()
return
if cols[-1].endswith("SimosTools\n"):
print("INFO: Header has already been fixed.")
f.close()
return
# read entire file to memory
f.seek(0)
data = f.readlines()
# apply modification
cols[-1] = 'SimosTools\n'
data[0] = ",".join(cols)
# write new file
f.seek(0)
f.writelines(data)
f.truncate()
# cleanup
del data
f.close()
if __name__ == '__main__':
files = []
for f in iglob('./simostools-*.csv'):
files.append(f)
num_files = len(files)
for i, f in enumerate(files):
print(f"[{i+1} / {num_files}]: {f}")
modify_header(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment