是否可以从Java检查给定文件/字符串的正确python语法(Is it possible to check for correct python syntax of a given file/str

编程入门 行业动态 更新时间:2024-10-24 17:30:27
是否可以从Java检查给定文件/字符串的正确python语法(Is it possible to check for correct python syntax of a given file/string from Java)

我正在用java编写一段代码,这部分代码处理python代码。 我只是感兴趣,如果有人在运行时检查python代码是否在语法上是正确的。 我实际上并不需要运行python代码,因为我正在编写一个程序,为项目的一部分教学目的生成它的小片段。

使用系统命令是实现这一目的的唯一方法吗?

I am writing a piece of code in java, part of this code deals with handling python code. I was just interested if anyone has come across a way of checking if the python code is syntactically correct during runtime. I don't actually need to run the python code, as i'm writing a program that generates small snippets of it for teaching purposes as part of a project.

Is using a system commands the only way to achieve this?

最满意答案

这可以通过Jython完成:

new org.python.util.PythonInterpreter().compile("python code here")

如果发现问题会抛出异常(很可能是org.python.core.PySyntaxError )

This can be done with Jython:

new org.python.util.PythonInterpreter().compile("python code here")

and an exception will be thrown if it finds a problem (likely org.python.core.PySyntaxError)

更多推荐

本文发布于:2023-08-07 20:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465533.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   语法   正确   文件   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!