question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Newline character as list delimiter?

See original GitHub issue

Cant figure out how to parse a line by line file (CSV, or other list…)

from pyleri import Choice, Grammar, Keyword, List, Optional, Ref, Regex, Sequence, Token
import os


class MyGrammar(Grammar):
	element = Regex(r"\w+")
	START = List(element, "\n", opt=True)


grammar = MyGrammar()
result = grammar.parse("word\nword\n").as_str()

print(result)

error at position 5, expecting:

Tried with combination of \n, \n, os.linesep…

No luck

Any idea?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
joentecommented, Dec 9, 2019

Maybe your use-case can be solved with Repeat instead of a List:

from pyleri import Repeat, Grammar, Regex

class MyGrammar(Grammar):
	element = Regex(r"\w+")
	START = Repeat(element)

grammar = MyGrammar()
print(grammar.parse("word\nword\n").as_str())  # parsed successfully

However, if it get’s more complicated you might run into to problems since pyleri handles all white-space (including line breaks) equally. This could be solved rather easy though, for example we could allow a user to define the white-space used in a grammar by RE_WHITESPACE, like we do with RE_KEYWORDS.

0reactions
joentecommented, Feb 11, 2021

@wqp89324, pyleri is currently using strip without any arguments for handling white-space between nodes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Split string using a newline delimiter with Python [duplicate]
str.split , by default, splits by all the whitespace characters. If the actual string has any other whitespace characters, you might want to...
Read more >
Python - Split String by Newline Character
To split a string by newline character in Python, pass the newline character "\n" as a delimiter to the split() function. It returns...
Read more >
Python Split String by New Line
We will use re package to split the string with one or more new line characters as delimiter. The regular expression that represents...
Read more >
Split a string on newline characters in Python | bobbyhadz
The str.splitlines method splits the string on newline characters and returns a list containing the lines in the string. The method does not...
Read more >
Python | Ways to split strings using newline delimiter
Given a string, write a Python program to split strings on the basis of newline delimiter. Given below are a few methods to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found