Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 包含空格时解析新行分隔字符串_Python_Parsing - Fatal编程技术网

Python 包含空格时解析新行分隔字符串

Python 包含空格时解析新行分隔字符串,python,parsing,Python,Parsing,我正在使用真正整洁的库来拆分字符串(作为更大语法定义的一部分)。sep_by在没有嵌入空格的情况下非常有效。基本上,我想按新行分割并获得所有字符,包括嵌入的空格或任何其他Unicode字符。示例: 作品: >>> parser = letter.at_least(1).concat().sep_by(string('\n')) >>> parser.parse('Smith\nFirefighter') ['Smith', 'Firefighter'] 不起

我正在使用真正整洁的库来拆分字符串(作为更大语法定义的一部分)。sep_by在没有嵌入空格的情况下非常有效。基本上,我想按新行分割并获得所有字符,包括嵌入的空格或任何其他Unicode字符。示例:

作品:

>>> parser = letter.at_least(1).concat().sep_by(string('\n'))
>>> parser.parse('Smith\nFirefighter')
['Smith', 'Firefighter']
不起作用:

>>> parser.parse('John Smith\nFire fighter')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\code\Parsing\Parsing\env\lib\site-packages\parsy\__init__.py", line 90, in parse
    (result, _) = (self << eof).parse_partial(stream)
  File "C:\code\Parsing\Parsing\env\lib\site-packages\parsy\__init__.py", line 104, in parse_partial
    raise ParseError(result.expected, stream, result.furthest)
parsy.ParseError: expected one of 'EOF', '\n', 'a letter' at 0:4
>parser.parse('John Smith\n Fire fighter')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\code\Parsing\Parsing\env\lib\site packages\parsy\\uuuu init\uuuu.py”,第90行,在parse中
(result,780;)=(self>>parser.parse('hello\u265ethere\nsir'))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\code\Parsing\Parsing\env\lib\site packages\parsy\\uuuu init\uuuu.py”,第90行,在parse中

(结果,)=(自我到目前为止的解决方案问题:

  • 字母
    仅匹配字母字符,而不是所有字符
  • whitespace
    匹配任何空格,因此如果您匹配
    letter | whitespace
    它将使用换行符,而不是拆分换行符
基本上,您需要一个“除换行符外的任何字符”解析器。最简单的方法是使用正则表达式:

>>> parser = regex(r"[^\n]+").sep_by(string('\n'))
>>> parser.parse("John Smith\nFirefigher")
['John Smith', 'Firefigher']

我通常发现使用Parsy,regex是构建最低级别部分的最简单方法。

到目前为止解决方案的问题:

  • 字母
    仅匹配字母字符,而不是所有字符
  • whitespace
    匹配任何空格,因此如果您匹配
    letter | whitespace
    它将使用换行符,而不是拆分换行符
基本上,您需要一个“除换行符外的任何字符”解析器。最简单的方法是使用正则表达式:

>>> parser = regex(r"[^\n]+").sep_by(string('\n'))
>>> parser.parse("John Smith\nFirefigher")
['John Smith', 'Firefigher']

我通常发现使用Parsy时,regex是构建最低级别部分的最简单方法。

出于好奇,您是否有理由不使用内置的
.split()
方法?只需执行
myStr.split('\n'))
只接受字母,因此失败。请尝试构建自定义。错误-我没有执行拆分,因为这是一个按顺序执行的语法分析器,基本上是在“下一段”上执行的Brian-没错!字母仅定义为ascii字符。我通过执行
allow=(字母|空格)来实现它parser=allow.at_least(1).concat().sep_by(string('\n'))
请发布单独的答案,我会将其标记为已回答。谢谢!感谢Paul添加parsy标记出于好奇您不使用内置的
.split()
方法的原因是什么?只需执行
myStr.split('\n'))
只接受字母,因此失败。请尝试构建自定义。错误-我没有执行拆分,因为这是一个按顺序执行的语法分析器,基本上是在“下一段”上执行的Brian-没错!字母仅定义为ascii字符。我通过执行
allow=(字母|空格)来实现它parser=allow.least(1).concat().sep_by(string('\n'))
请发布单独的答案,我会将其标记为已回答。谢谢!感谢Paul添加parsy标记谢谢。我是通过执行
any_char.many().concat()
并通过
yield
将其转换为一个变量,然后执行python换行
split()
在它上面。你的解决方案当然更好。谢谢!谢谢。我通过执行
any_char.many().concat()
并通过
yield
将其转换为变量,然后在它上面执行python换行
split()
来实现这一点。你的解决方案当然更好。谢谢!