For Loop Parsing with %
Just started learning Python a month ago. I'm stumped on an issue.
Trying to update a dictionary using a for loop. I would like to store the variable using the for loop's variable name as key and the variable value as the dictionary's value. I'm trying to use % to make it a one liner. Here's what I have so far:
grbl_parser_d = {
'a': 'null', # Motion Mode
'b': 'null', # Coordinate System Select
'c': 'null' # Plane Select
}
grbl_out = [GC:G0 G54 G17]
def filtergrblparser():
global grbl_parser_d
for l, r in [grbl_out.strip('').split(':')]:
for a, b, c in [r.split(' ')]:
# grbl_parser_d.update({'%': x}) % x
grbl_parser_d.update({'a': a})
grbl_parser_d.update({'b': b})
grbl_parser_d.update({'c': c})
The 'grbl_out' variable is the output from an Arduino.
Trying to use something like: grbl_parser_d.update({'%': a}) % a.name
'a.name' would be the for loop's variable name, not the value. Is this even possible? Any other suggestions and tips to clean up the code would also be greatly appreciated. Thanks!
python parsing
add a comment |
Just started learning Python a month ago. I'm stumped on an issue.
Trying to update a dictionary using a for loop. I would like to store the variable using the for loop's variable name as key and the variable value as the dictionary's value. I'm trying to use % to make it a one liner. Here's what I have so far:
grbl_parser_d = {
'a': 'null', # Motion Mode
'b': 'null', # Coordinate System Select
'c': 'null' # Plane Select
}
grbl_out = [GC:G0 G54 G17]
def filtergrblparser():
global grbl_parser_d
for l, r in [grbl_out.strip('').split(':')]:
for a, b, c in [r.split(' ')]:
# grbl_parser_d.update({'%': x}) % x
grbl_parser_d.update({'a': a})
grbl_parser_d.update({'b': b})
grbl_parser_d.update({'c': c})
The 'grbl_out' variable is the output from an Arduino.
Trying to use something like: grbl_parser_d.update({'%': a}) % a.name
'a.name' would be the for loop's variable name, not the value. Is this even possible? Any other suggestions and tips to clean up the code would also be greatly appreciated. Thanks!
python parsing
add a comment |
Just started learning Python a month ago. I'm stumped on an issue.
Trying to update a dictionary using a for loop. I would like to store the variable using the for loop's variable name as key and the variable value as the dictionary's value. I'm trying to use % to make it a one liner. Here's what I have so far:
grbl_parser_d = {
'a': 'null', # Motion Mode
'b': 'null', # Coordinate System Select
'c': 'null' # Plane Select
}
grbl_out = [GC:G0 G54 G17]
def filtergrblparser():
global grbl_parser_d
for l, r in [grbl_out.strip('').split(':')]:
for a, b, c in [r.split(' ')]:
# grbl_parser_d.update({'%': x}) % x
grbl_parser_d.update({'a': a})
grbl_parser_d.update({'b': b})
grbl_parser_d.update({'c': c})
The 'grbl_out' variable is the output from an Arduino.
Trying to use something like: grbl_parser_d.update({'%': a}) % a.name
'a.name' would be the for loop's variable name, not the value. Is this even possible? Any other suggestions and tips to clean up the code would also be greatly appreciated. Thanks!
python parsing
Just started learning Python a month ago. I'm stumped on an issue.
Trying to update a dictionary using a for loop. I would like to store the variable using the for loop's variable name as key and the variable value as the dictionary's value. I'm trying to use % to make it a one liner. Here's what I have so far:
grbl_parser_d = {
'a': 'null', # Motion Mode
'b': 'null', # Coordinate System Select
'c': 'null' # Plane Select
}
grbl_out = [GC:G0 G54 G17]
def filtergrblparser():
global grbl_parser_d
for l, r in [grbl_out.strip('').split(':')]:
for a, b, c in [r.split(' ')]:
# grbl_parser_d.update({'%': x}) % x
grbl_parser_d.update({'a': a})
grbl_parser_d.update({'b': b})
grbl_parser_d.update({'c': c})
The 'grbl_out' variable is the output from an Arduino.
Trying to use something like: grbl_parser_d.update({'%': a}) % a.name
'a.name' would be the for loop's variable name, not the value. Is this even possible? Any other suggestions and tips to clean up the code would also be greatly appreciated. Thanks!
python parsing
python parsing
edited Nov 25 '18 at 8:30
Andrew McDowell
1,9321417
1,9321417
asked Nov 25 '18 at 8:22
LMS-SmokeLMS-Smoke
62
62
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You don't need loops for this, and I wouldn't try to cram it onto one line. Here's a simple function that should do what you want.
def filtergrblparser(grbl_out):
l, r = grbl_out.strip('').split(':')
a, b, c = r.split(' ')
grbl_parser_d = {
'a': a, # Motion Mode
'b': b, # Coordinate System Select
'c': c # Plane Select
}
return grbl_parser_d
# I'm assuming you meant this to be a string
grbl_out = "[GC:G0 G54 G17]"
grbl_parser_d = filtergrblparser(grbl_out)
print(grbl_parser_d)
# {'a': 'G0', 'b': 'G54', 'c': 'G17'}
add a comment |
This is generally a bad idea, but it could be done with another for
loop.
# it's not clear why you're throwing this in a list just to iterate once over it
l, r = grbl_out.strip('').split(':')
a, b, c = r.split(' ')
for k in ['a', 'b', 'c']:
grbl_parser_d[k] = vars()[k]
But really it looks like you're trying to do:
grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' ')))
Which is probably best written as:
l, r = grbl_out.strip('').split(':')
grbl_parser_d = dict(zip('abc', r.split(' ')))
Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.
– LMS-Smoke
Nov 25 '18 at 9:02
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465804%2ffor-loop-parsing-with%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't need loops for this, and I wouldn't try to cram it onto one line. Here's a simple function that should do what you want.
def filtergrblparser(grbl_out):
l, r = grbl_out.strip('').split(':')
a, b, c = r.split(' ')
grbl_parser_d = {
'a': a, # Motion Mode
'b': b, # Coordinate System Select
'c': c # Plane Select
}
return grbl_parser_d
# I'm assuming you meant this to be a string
grbl_out = "[GC:G0 G54 G17]"
grbl_parser_d = filtergrblparser(grbl_out)
print(grbl_parser_d)
# {'a': 'G0', 'b': 'G54', 'c': 'G17'}
add a comment |
You don't need loops for this, and I wouldn't try to cram it onto one line. Here's a simple function that should do what you want.
def filtergrblparser(grbl_out):
l, r = grbl_out.strip('').split(':')
a, b, c = r.split(' ')
grbl_parser_d = {
'a': a, # Motion Mode
'b': b, # Coordinate System Select
'c': c # Plane Select
}
return grbl_parser_d
# I'm assuming you meant this to be a string
grbl_out = "[GC:G0 G54 G17]"
grbl_parser_d = filtergrblparser(grbl_out)
print(grbl_parser_d)
# {'a': 'G0', 'b': 'G54', 'c': 'G17'}
add a comment |
You don't need loops for this, and I wouldn't try to cram it onto one line. Here's a simple function that should do what you want.
def filtergrblparser(grbl_out):
l, r = grbl_out.strip('').split(':')
a, b, c = r.split(' ')
grbl_parser_d = {
'a': a, # Motion Mode
'b': b, # Coordinate System Select
'c': c # Plane Select
}
return grbl_parser_d
# I'm assuming you meant this to be a string
grbl_out = "[GC:G0 G54 G17]"
grbl_parser_d = filtergrblparser(grbl_out)
print(grbl_parser_d)
# {'a': 'G0', 'b': 'G54', 'c': 'G17'}
You don't need loops for this, and I wouldn't try to cram it onto one line. Here's a simple function that should do what you want.
def filtergrblparser(grbl_out):
l, r = grbl_out.strip('').split(':')
a, b, c = r.split(' ')
grbl_parser_d = {
'a': a, # Motion Mode
'b': b, # Coordinate System Select
'c': c # Plane Select
}
return grbl_parser_d
# I'm assuming you meant this to be a string
grbl_out = "[GC:G0 G54 G17]"
grbl_parser_d = filtergrblparser(grbl_out)
print(grbl_parser_d)
# {'a': 'G0', 'b': 'G54', 'c': 'G17'}
answered Nov 25 '18 at 8:36
CyphaseCyphase
8,39011627
8,39011627
add a comment |
add a comment |
This is generally a bad idea, but it could be done with another for
loop.
# it's not clear why you're throwing this in a list just to iterate once over it
l, r = grbl_out.strip('').split(':')
a, b, c = r.split(' ')
for k in ['a', 'b', 'c']:
grbl_parser_d[k] = vars()[k]
But really it looks like you're trying to do:
grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' ')))
Which is probably best written as:
l, r = grbl_out.strip('').split(':')
grbl_parser_d = dict(zip('abc', r.split(' ')))
Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.
– LMS-Smoke
Nov 25 '18 at 9:02
add a comment |
This is generally a bad idea, but it could be done with another for
loop.
# it's not clear why you're throwing this in a list just to iterate once over it
l, r = grbl_out.strip('').split(':')
a, b, c = r.split(' ')
for k in ['a', 'b', 'c']:
grbl_parser_d[k] = vars()[k]
But really it looks like you're trying to do:
grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' ')))
Which is probably best written as:
l, r = grbl_out.strip('').split(':')
grbl_parser_d = dict(zip('abc', r.split(' ')))
Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.
– LMS-Smoke
Nov 25 '18 at 9:02
add a comment |
This is generally a bad idea, but it could be done with another for
loop.
# it's not clear why you're throwing this in a list just to iterate once over it
l, r = grbl_out.strip('').split(':')
a, b, c = r.split(' ')
for k in ['a', 'b', 'c']:
grbl_parser_d[k] = vars()[k]
But really it looks like you're trying to do:
grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' ')))
Which is probably best written as:
l, r = grbl_out.strip('').split(':')
grbl_parser_d = dict(zip('abc', r.split(' ')))
This is generally a bad idea, but it could be done with another for
loop.
# it's not clear why you're throwing this in a list just to iterate once over it
l, r = grbl_out.strip('').split(':')
a, b, c = r.split(' ')
for k in ['a', 'b', 'c']:
grbl_parser_d[k] = vars()[k]
But really it looks like you're trying to do:
grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' ')))
Which is probably best written as:
l, r = grbl_out.strip('').split(':')
grbl_parser_d = dict(zip('abc', r.split(' ')))
answered Nov 25 '18 at 8:34
Adam SmithAdam Smith
34.6k63276
34.6k63276
Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.
– LMS-Smoke
Nov 25 '18 at 9:02
add a comment |
Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.
– LMS-Smoke
Nov 25 '18 at 9:02
Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.
– LMS-Smoke
Nov 25 '18 at 9:02
Thank you! grbl_parser_d = dict(zip('abc', grbl_out.strip('').split(':')[1].split(' '))) is exactly what I was looking for.
– LMS-Smoke
Nov 25 '18 at 9:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465804%2ffor-loop-parsing-with%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown