Last active
September 3, 2025 02:10
-
-
Save berkakinci/c09641a9f2bb54007b38ab6e0a1f0137 to your computer and use it in GitHub Desktop.
Convert Some 3D Printer (or other) G Code to CSV format for data analysis. I used this to diagnose a failing 3D print due to pathological layers over-utilizing retractions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import sys | |
| import re | |
| import csv | |
| # Define column headers for common G-code fields | |
| parameters = ["X", "Y", "Z", "E", "F", "S", "T"] | |
| fields = ["Command", *parameters, "Other", "Comment"] | |
| def parsegcode(tokens): | |
| """Parse tokens into parameter dictionary.""" | |
| out = {name : "" for name in parameters + ["Command", "Other"]} | |
| if not tokens: | |
| return out | |
| # First token is the command | |
| out["Command"] = tokens.pop(0) | |
| # Rest are parameters | |
| for token in tokens: | |
| # match first letter(s) as name, rest as value | |
| match = re.match(r"([A-Za-z]+)(.*)", token) | |
| if match: | |
| name, value = match.groups() | |
| name = name.upper() | |
| if name in parameters: | |
| out[name] += str(value) | |
| else: | |
| out["Other"] += token + " " | |
| else: | |
| out["Other"] += token + " " | |
| return out | |
| csvwriter = csv.DictWriter(sys.stdout, | |
| fieldnames=fields) | |
| csvwriter.writeheader() | |
| for line in sys.stdin: | |
| line = line.rstrip("\n") | |
| tokens, _, comment = line.partition(";") | |
| tokens = tokens.split() | |
| outvals = parsegcode(tokens) | |
| outvals["Comment"] = comment | |
| csvwriter.writerow(outvals) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output looks like:
And with some Excel formulas and Pivot tables, it's possible to find problem areas like:
