pyparsing: ignore any token that doesn't match












1














I have this file from a game that I'm trying to parse. Here is an excerpt:



    <stage> id: 50  #Survival Stage
<phase> bound: 1500 # phase 0 bandit
music: bgmstage4.wma
id: 122 x: 100 #milk ratio: 1
id: 30 hp: 50 times: 1
id: 30 hp: 50 times: 1 ratio: 0.7
id: 30 hp: 50 times: 1 ratio: 0.3
<phase_end>
<stage_end>


The # denotes a comment, but only to human readers, not to the game's parser. The first two comments are to the end of the line, but the ratio: 1 after #milk is not part of the comment, it actually counts. I think the game's parser ignores any tokens it can't understand. Is there a way to do this in pyparsing?



I tried using parser.ignore(pp.Word(pp.printables)) but that makes it skip over everything. Here's my code so far:



import pyparsing as pp

txt = """
<stage> id: 50 #Survival Stage
<phase> bound: 1500 # phase 0 bandit
music: bgmstage4.wma
id: 122 x: 100 #milk ratio: 1
id: 30 hp: 50 times: 1
id: 30 hp: 50 times: 1 ratio: 0.7
id: 30 hp: 50 times: 1 ratio: 0.3
<phase_end>
<stage_end>
"""

phase = pp.Literal('<phase>')
stage = pp.Literal('<stage>') + pp.Literal('id:') + pp.Word(pp.nums)('id') + pp.OneOrMore(phase)
parser = stage

parser.ignore(pp.Word(pp.printables))

print(parser.parseString(txt).dump())









share|improve this question






















  • Can you say what game this is?
    – user2357112
    Nov 14 '18 at 0:26










  • possible duplicate : stackoverflow.com/questions/43075547/…
    – Joran Beasley
    Nov 14 '18 at 0:26










  • @user2357112 Little Fighter 2
    – zort
    Nov 14 '18 at 1:03










  • @JoranBeasley The answer to that question doesn't work for me because I want to parse the whole file.
    – zort
    Nov 14 '18 at 1:04










  • yeah you would parse each line ... or write a rule to ignore comment tokens
    – Joran Beasley
    Nov 14 '18 at 2:16
















1














I have this file from a game that I'm trying to parse. Here is an excerpt:



    <stage> id: 50  #Survival Stage
<phase> bound: 1500 # phase 0 bandit
music: bgmstage4.wma
id: 122 x: 100 #milk ratio: 1
id: 30 hp: 50 times: 1
id: 30 hp: 50 times: 1 ratio: 0.7
id: 30 hp: 50 times: 1 ratio: 0.3
<phase_end>
<stage_end>


The # denotes a comment, but only to human readers, not to the game's parser. The first two comments are to the end of the line, but the ratio: 1 after #milk is not part of the comment, it actually counts. I think the game's parser ignores any tokens it can't understand. Is there a way to do this in pyparsing?



I tried using parser.ignore(pp.Word(pp.printables)) but that makes it skip over everything. Here's my code so far:



import pyparsing as pp

txt = """
<stage> id: 50 #Survival Stage
<phase> bound: 1500 # phase 0 bandit
music: bgmstage4.wma
id: 122 x: 100 #milk ratio: 1
id: 30 hp: 50 times: 1
id: 30 hp: 50 times: 1 ratio: 0.7
id: 30 hp: 50 times: 1 ratio: 0.3
<phase_end>
<stage_end>
"""

phase = pp.Literal('<phase>')
stage = pp.Literal('<stage>') + pp.Literal('id:') + pp.Word(pp.nums)('id') + pp.OneOrMore(phase)
parser = stage

parser.ignore(pp.Word(pp.printables))

print(parser.parseString(txt).dump())









share|improve this question






















  • Can you say what game this is?
    – user2357112
    Nov 14 '18 at 0:26










  • possible duplicate : stackoverflow.com/questions/43075547/…
    – Joran Beasley
    Nov 14 '18 at 0:26










  • @user2357112 Little Fighter 2
    – zort
    Nov 14 '18 at 1:03










  • @JoranBeasley The answer to that question doesn't work for me because I want to parse the whole file.
    – zort
    Nov 14 '18 at 1:04










  • yeah you would parse each line ... or write a rule to ignore comment tokens
    – Joran Beasley
    Nov 14 '18 at 2:16














1












1








1







I have this file from a game that I'm trying to parse. Here is an excerpt:



    <stage> id: 50  #Survival Stage
<phase> bound: 1500 # phase 0 bandit
music: bgmstage4.wma
id: 122 x: 100 #milk ratio: 1
id: 30 hp: 50 times: 1
id: 30 hp: 50 times: 1 ratio: 0.7
id: 30 hp: 50 times: 1 ratio: 0.3
<phase_end>
<stage_end>


The # denotes a comment, but only to human readers, not to the game's parser. The first two comments are to the end of the line, but the ratio: 1 after #milk is not part of the comment, it actually counts. I think the game's parser ignores any tokens it can't understand. Is there a way to do this in pyparsing?



I tried using parser.ignore(pp.Word(pp.printables)) but that makes it skip over everything. Here's my code so far:



import pyparsing as pp

txt = """
<stage> id: 50 #Survival Stage
<phase> bound: 1500 # phase 0 bandit
music: bgmstage4.wma
id: 122 x: 100 #milk ratio: 1
id: 30 hp: 50 times: 1
id: 30 hp: 50 times: 1 ratio: 0.7
id: 30 hp: 50 times: 1 ratio: 0.3
<phase_end>
<stage_end>
"""

phase = pp.Literal('<phase>')
stage = pp.Literal('<stage>') + pp.Literal('id:') + pp.Word(pp.nums)('id') + pp.OneOrMore(phase)
parser = stage

parser.ignore(pp.Word(pp.printables))

print(parser.parseString(txt).dump())









share|improve this question













I have this file from a game that I'm trying to parse. Here is an excerpt:



    <stage> id: 50  #Survival Stage
<phase> bound: 1500 # phase 0 bandit
music: bgmstage4.wma
id: 122 x: 100 #milk ratio: 1
id: 30 hp: 50 times: 1
id: 30 hp: 50 times: 1 ratio: 0.7
id: 30 hp: 50 times: 1 ratio: 0.3
<phase_end>
<stage_end>


The # denotes a comment, but only to human readers, not to the game's parser. The first two comments are to the end of the line, but the ratio: 1 after #milk is not part of the comment, it actually counts. I think the game's parser ignores any tokens it can't understand. Is there a way to do this in pyparsing?



I tried using parser.ignore(pp.Word(pp.printables)) but that makes it skip over everything. Here's my code so far:



import pyparsing as pp

txt = """
<stage> id: 50 #Survival Stage
<phase> bound: 1500 # phase 0 bandit
music: bgmstage4.wma
id: 122 x: 100 #milk ratio: 1
id: 30 hp: 50 times: 1
id: 30 hp: 50 times: 1 ratio: 0.7
id: 30 hp: 50 times: 1 ratio: 0.3
<phase_end>
<stage_end>
"""

phase = pp.Literal('<phase>')
stage = pp.Literal('<stage>') + pp.Literal('id:') + pp.Word(pp.nums)('id') + pp.OneOrMore(phase)
parser = stage

parser.ignore(pp.Word(pp.printables))

print(parser.parseString(txt).dump())






python pyparsing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 0:23









zort

163




163












  • Can you say what game this is?
    – user2357112
    Nov 14 '18 at 0:26










  • possible duplicate : stackoverflow.com/questions/43075547/…
    – Joran Beasley
    Nov 14 '18 at 0:26










  • @user2357112 Little Fighter 2
    – zort
    Nov 14 '18 at 1:03










  • @JoranBeasley The answer to that question doesn't work for me because I want to parse the whole file.
    – zort
    Nov 14 '18 at 1:04










  • yeah you would parse each line ... or write a rule to ignore comment tokens
    – Joran Beasley
    Nov 14 '18 at 2:16


















  • Can you say what game this is?
    – user2357112
    Nov 14 '18 at 0:26










  • possible duplicate : stackoverflow.com/questions/43075547/…
    – Joran Beasley
    Nov 14 '18 at 0:26










  • @user2357112 Little Fighter 2
    – zort
    Nov 14 '18 at 1:03










  • @JoranBeasley The answer to that question doesn't work for me because I want to parse the whole file.
    – zort
    Nov 14 '18 at 1:04










  • yeah you would parse each line ... or write a rule to ignore comment tokens
    – Joran Beasley
    Nov 14 '18 at 2:16
















Can you say what game this is?
– user2357112
Nov 14 '18 at 0:26




Can you say what game this is?
– user2357112
Nov 14 '18 at 0:26












possible duplicate : stackoverflow.com/questions/43075547/…
– Joran Beasley
Nov 14 '18 at 0:26




possible duplicate : stackoverflow.com/questions/43075547/…
– Joran Beasley
Nov 14 '18 at 0:26












@user2357112 Little Fighter 2
– zort
Nov 14 '18 at 1:03




@user2357112 Little Fighter 2
– zort
Nov 14 '18 at 1:03












@JoranBeasley The answer to that question doesn't work for me because I want to parse the whole file.
– zort
Nov 14 '18 at 1:04




@JoranBeasley The answer to that question doesn't work for me because I want to parse the whole file.
– zort
Nov 14 '18 at 1:04












yeah you would parse each line ... or write a rule to ignore comment tokens
– Joran Beasley
Nov 14 '18 at 2:16




yeah you would parse each line ... or write a rule to ignore comment tokens
– Joran Beasley
Nov 14 '18 at 2:16












1 Answer
1






active

oldest

votes


















1














It turns out in the stock game file only the ratio: keyword ever appears after a #, so I used that to define the end of a comment, like so:



parser.ignore(Suppress('#') + SkipTo(MatchFirst([FollowedBy('ratio:'), LineEnd()])))





share|improve this answer





















  • Sounds like a decent compromise - well done!
    – PaulMcG
    Nov 15 '18 at 23:50











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291414%2fpyparsing-ignore-any-token-that-doesnt-match%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














It turns out in the stock game file only the ratio: keyword ever appears after a #, so I used that to define the end of a comment, like so:



parser.ignore(Suppress('#') + SkipTo(MatchFirst([FollowedBy('ratio:'), LineEnd()])))





share|improve this answer





















  • Sounds like a decent compromise - well done!
    – PaulMcG
    Nov 15 '18 at 23:50
















1














It turns out in the stock game file only the ratio: keyword ever appears after a #, so I used that to define the end of a comment, like so:



parser.ignore(Suppress('#') + SkipTo(MatchFirst([FollowedBy('ratio:'), LineEnd()])))





share|improve this answer





















  • Sounds like a decent compromise - well done!
    – PaulMcG
    Nov 15 '18 at 23:50














1












1








1






It turns out in the stock game file only the ratio: keyword ever appears after a #, so I used that to define the end of a comment, like so:



parser.ignore(Suppress('#') + SkipTo(MatchFirst([FollowedBy('ratio:'), LineEnd()])))





share|improve this answer












It turns out in the stock game file only the ratio: keyword ever appears after a #, so I used that to define the end of a comment, like so:



parser.ignore(Suppress('#') + SkipTo(MatchFirst([FollowedBy('ratio:'), LineEnd()])))






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 14:43









zort

163




163












  • Sounds like a decent compromise - well done!
    – PaulMcG
    Nov 15 '18 at 23:50


















  • Sounds like a decent compromise - well done!
    – PaulMcG
    Nov 15 '18 at 23:50
















Sounds like a decent compromise - well done!
– PaulMcG
Nov 15 '18 at 23:50




Sounds like a decent compromise - well done!
– PaulMcG
Nov 15 '18 at 23:50


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291414%2fpyparsing-ignore-any-token-that-doesnt-match%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word