Skip to content

Instantly share code, notes, and snippets.

@berkakinci
Last active September 3, 2025 02:10
Show Gist options
  • Select an option

  • Save berkakinci/c09641a9f2bb54007b38ab6e0a1f0137 to your computer and use it in GitHub Desktop.

Select an option

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.
#!/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)
@berkakinci
Copy link
Author

Output looks like:

Command X Y Z E F S T Other Comment
M109           195     Finish heating the nozzle
G1     2   3000       Move Z Axis up little to prevent scratching of Heat Bed
G1 0.1 20 0.3   5000       Move to start position
G1 0.1 200 0.3 15 1500       Draw the first line

And with some Excel formulas and Pivot tables, it's possible to find problem areas like:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment