GcodeParser
A simple gcode parser that can be used to parse a gcode file into python GcodeLine objects.
The structure of the python object is:
G1 X10 Y-2.5 ; this is a comment
GcodeLine(
command = ('G', 1),
params = {'X': 10, 'Y': -2.5},
comment = 'this is a comment',
line_index = 0,
type = Commands.MOVE # Commands.MOVE, Commands.COMMENT, Commands.OTHER, Commands.TOOLCHANGE
)
Install
pip install gcodeparser
Alternatively:
pip install -e "git+https://github.com/AndyEveritt/GcodeParser.git@master#egg=gcodeparser"
Usage
from gcodeparser import parse_gcode_lines
# Recommended: iterate over lines in a file
with open('my_gcode.gcode', 'r') as f: # note that file should be open during iteration
for line in parse_gcode_lines(f, include_comments=False):
print(line)
# Alternative: open gcode file and parse lines into a list without iteration
with open('my_gcode.gcode', 'r') as f:
lines = list(parse_gcode_lines(f, include_comments=False))
# Also, we can convert string to parsed lines
with open('my_gcode.gcode', 'r') as f:
gcode = f.read()
lines = list(parse_gcode_lines(gcode, include_comments=False))
output:
[GcodeLine(command=('G', 10), params={'P': 0, 'R': 0, 'S': 0}, comment='sets the standby temperature'),
GcodeLine(command=('G', 29), params={'S': 1}, comment=''),
GcodeLine(command=('T', 0), params={}, comment=''),
GcodeLine(command=('G', 21), params={}, comment='set units to millimeters'),
GcodeLine(command=('G', 90), params={}, comment='use absolute coordinates'),
GcodeLine(command=('M', 83), params={}, comment='use relative distances for extrusion'),
GcodeLine(command=('G', 1), params={'E': -0.6, 'F': 3600.0}, comment=''),
GcodeLine(command=('G', 1), params={'Z': 0.45, 'F': 7800.0}, comment=''),
GcodeLine(command=('G', 1), params={'Z': 2.35}, comment=''),
GcodeLine(command=('G', 1), params={'X': 119.575, 'Y': 89.986}, comment=''),
GcodeLine(command=('G', 1), params={'Z': 0.45}, comment=''),
GcodeLine(command=('G', 1), params={'E': 0.6, 'F': 3600.0}, comment=''),
GcodeLine(command=('G', 1), params={'F': 1800.0}, comment=''),
GcodeLine(command=('G', 1), params={'X': 120.774, 'Y': 88.783, 'E': 0.17459}, comment=''),
GcodeLine(command=('G', 1), params={'X': 121.692, 'Y': 88.145, 'E': 0.11492}, comment=''),
GcodeLine(command=('G', 1), params={'X': 122.7, 'Y': 87.638, 'E': 0.11596}, comment=''),
GcodeLine(command=('G', 1), params={'X': 123.742, 'Y': 87.285, 'E': 0.11317}, comment=''),
...
]
Include Comments
parse_gcode_lines() takes a second argument called include_comments which defaults to False. If this is set to True then any line from the gcode file which only contains a comment will also be included in the output.
from gcodeparser import parse_gcode_lines
gcode = """G1 X1 ; this comment is always included
; this comment will only be included if `include_comments=True`"""
lines = list(parse_gcode_lines(gcode, include_comments=True))
If include_comments is True then the comment line will be in the form of:
GcodeLine(
command = (';', None),
params = {},
comment = 'this comment will only be included if `include_comments=True`',
)
Convert Command Tuple to String
The GcodeLine class has a property command_str which will return the command tuple as a string. ie ('G', 91) -> "G91".
Changing back to Gcode String
The GcodeLine class has a property gcode_str which will return the equivalent gcode string.
This was called
to_gcode()in version 0.0.6 and before.
Retrieving Params
To retrieve a param, use the method get_param(param: str, return_type=None, default=None) which
returns the value of the param if it exists, otherwise it will the default value.
If return_type is set, the return value will be type cast.
line.get_param('X')
Updating Params
To update a param, use the method update_param(param: str, value: int | float)
line.update_param('X', 10)
If the param does not exist, it will return None else it will return the updated value.
Deleting Params
To delete a param, use the method delete_param(param: str)
line.delete_param('X')
Converting to DataFrames
If for whatever reason you want to convert your list of GcodeLine objects into a pandas dataframe:
from gcodeparser import parse_gcode_lines
import pandas as pd
with open('my_gcode.gcode', 'r') as f:
lines = list(parse_gcode_lines(f))
df = pd.DataFrame(lines)
Metadata
Release files for gcodeparser 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| gcodeparser-0.3.0.tar.gz | 8.3 kB | Without provenance | Details for gcodeparser-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gcodeparser-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for gcodeparser-0.3.0-py3-none-any.whl |
Total release size: 18.2 kB
Release files / gcodeparser-0.3.0.tar.gz
| Download URL | gcodeparser-0.3.0.tar.gz |
|---|---|
| Size | 8.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9c202527e7255ff9bd95c74c7fd51fc9a62cc73ccf37b4b03a90e3b62ab1630b
|
|
BLAKE2b-256 checksum How to use checksums |
4b8dd77496a4c04aebc910c4c2e3532335fb2ef70f925b74376b9dd8082a702c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / gcodeparser-0.3.0-py3-none-any.whl
| Download URL | gcodeparser-0.3.0-py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4b35d9f9fea16639ee30cb9eb3b2c5fd0d521639edf55e8235232446d791c603
|
|
BLAKE2b-256 checksum How to use checksums |
acbfb595b93e43adcac3b004ef9dde26fade62935b8e14701cec1e2a35577acc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for gcodeparser 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.