gpt4 book ai didi

python - lark : Parsing : Implementing import file?

转载 作者:行者123 更新时间:2023-12-01 06:56:28 24 4
gpt4 key购买 nike

如何实现可以导入文件并仍然使用 LARK 解析它的语法?

例如:

@import file.txt
.....

最佳答案

我找到了一个看起来相关的 GitHub,这就是您要找的吗? https://github.com/lark-parser/lark

from lark import Lark
with open('file_to_read.txt', 'r') as file:
data = file.read().replace('\n', '') #assumes you want to remove \n
l = Lark('''start: WORD "," WORD "!"
%import common.WORD // imports from terminal library
%ignore " " // Disregard spaces in text
''')

print( l.parse("Hello, World!") )
print( l.parse(data) )

如果你想打开该文件并将其用作 lark

from lark import Lark
with open('file_to_read.txt', 'r') as file:
data = file.read().replace('\n', '') #assumes you want to remove \n
l = Lark(data)

print( l.parse("Hello, World!") )
print( l.parse("your string to parse") )

关于python - lark : Parsing : Implementing import file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58783994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com