VB.n中流的下一个字符串

2024-06-16 13:40:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些python文件。我需要从那里得到所有的课程,并列出一个清单。 就像我必须用streamreader阅读然后

导入操作块

我必须获取字符串ActionBlock并在列表中显示它。我希望我能做到,但我被困在这一点上。有什么建议吗?多谢各位


Tags: 文件字符串列表建议课程streamreaderactionblock
1条回答
网友
1楼 · 发布于 2024-06-16 13:40:53

您可以使用正则表达式查找您感兴趣的部分

下面的代码

Dim path = "c:\path\to\your\file.py"
Dim content = File.ReadAllText(path)

Dim matchClass = "class (?<m>\w+)(:|\()+"
Dim matchImport = "(^|from \w+ )import ((?<m>\w+), )*(?<m>\w+)"

Dim result = Regex.Matches(content, String.Format("({0}|{1})", matchClass, matchImport), RegexOptions.Multiline) _
                  .Cast(Of Match) _
                  .SelectMany(Function(m) m.Groups("m").Captures.Cast(Of Capture).Select(Function(c) c.Value)) _
                  .ToList() 

will,给定一个文本文件,如

import os 
import math 
from time import clock 
from random import randint 
import DataArchiving 
import TABasicFunctions 
import HWDataConveterGate 
import GeneralTestDataMapping
from something import FirstClass, SecondClass

def foo():
    pass

def bar():
    pass

class ClassOne(object):
    class NestedClass:
        pass

    def thisisnotaclass(self):
        v = [x.class for x in self]
        v = [x.someimport for x in self]

class ClassTwo:
    pass

class Class3:
    pass

def main():
    pass

if __name__ == '__main__':
    main()

创建一个如下所示的列表:

enter image description here

相关问题 更多 >