gpt4 book ai didi

python - 如何使用 Python 在文件中查找特殊单词?

转载 作者:行者123 更新时间:2023-11-28 21:48:18 25 4
gpt4 key购买 nike

我在一个目录中有一堆 .java 文件,我想通过 python 代码将它们全部编译为 .class 文件。

如您所知,Javac 命令行工具是我必须使用的工具,它要求.java 文件的名称与类名相同。不幸的是,对于我的 .java 文件,它不是。我的意思是他们有不同的随机名称,这些名称不等于他们的类(class)名称。

所以我需要从.java 文件的内容中提取类名。如果指定了类定义行就很简单,但事实并非如此。 .java 文件可能在顶部包含一些注释,这些注释也可能包含 classpackage 词。

问题是如何提取每个文件的包名和类名?

例如这是其中一个的内容:

//This is a sample package that its class name is HelloWorldApplet. in this package we blah blah blah and this class blah blah blah.
package helloWorldPackage;
//This is another comment that may or may not have the word "package" and "class" inside.
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;
/* this is also a multi line comment. blah blah blah package, blah blah blah package ... */
public class HelloWorldApplet extends Applet
{
private static final byte[] helloWorld = {(byte)'H',(byte)'e',(byte)'l',(byte)'l',(byte)'o',(byte)' ',(byte)'W',(byte)'o',(byte)'r',(byte)'l',(byte)'d',};
private static final byte HW_CLA = (byte)0x80;
private static final byte HW_INS = (byte)0x00;

public static void install(byte[] bArray, short bOffset, byte bLength)
{
new HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
}

public void process(APDU apdu)
{
if (selectingApplet())
{
return;
}

byte[] buffer = apdu.getBuffer();
byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);

if (CLA != HW_CLA)
{
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}

switch ( INS )
{
case HW_INS:
getHelloWorld( apdu );
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}

private void getHelloWorld( APDU apdu)
{
byte[] buffer = apdu.getBuffer();
short length = (short) helloWorld.length;
Util.arrayCopyNonAtomic(helloWorld, (short)0, buffer, (short)0, (short) length);
apdu.setOutgoingAndSend((short)0, length);
}
}

如何提取每个文件的包名(即 helloWorldPackage)和类名(即 HelloWorldApplet)?

请注意,.java 文件内部可能有不同的类,但我只需要扩展 Applet 的类的名称。

更新:

我尝试了以下方法,但它们没有用(Python 2.7.10):

import re

prgFile = open(r"yourFile\New Text Document.txt","r")
contents = prgFile.read()

x = re.match(r"(?<=class)\b.*\b(?=extends Applet)",contents)
print x
x = re.match(r"^(public)+",contents)
print x
x = re.match(r"^package ([^;\n]+)",contents)
print x
x = re.match(r"(?<=^public class )\b.*\b(?= extends Applet)",contents)
print x

输出:

>>> ================================ RESTART ================================
>>>
None
None
None
None
>>>

最佳答案

在许多情况下,一个简单的正则表达式就可以了。

如果您想 100% 确定我建议使用像 javalang 这样的成熟的 Java 解析器解析每个文件,然后遍历 AST 以提取类名。

有点像

import glob
import javalang

# look at all .java files in the working directory
for fname in glob.glob("*.java"):
# load the sourcecode
with open(fname) as inf:
sourcecode = inf.read()

try:
# parse it to an Abstract Syntax Tree
tree = javalang.parse.parse(sourcecode)
# get package name
pkg = tree.package.name

# look at all class declarations
for path, node in tree.filter(javalang.tree.ClassDeclaration):
# if class extends Applet
if node.extends.name == 'Applet':
# print the class name
print("{}: package {}, main class is {}".format(fname, pkg, node.name))

except javalang.parser.JavaSyntaxError as je:
# report any files which don't parse properly
print("Error parsing {}: {}".format(fname, je))

给出

sample.java: package helloWorldPackage, main class is HelloWorldApplet

关于python - 如何使用 Python 在文件中查找特殊单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35538814/

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