定位一行文本中的单词

发布于 2025-01-11 16:48:59 字数 726 浏览 0 评论 0原文

我在一个文本文件中找到了一行,如下所示:

FLAGS                    = WORD1 WORD2 WORD3

我正在阅读几个文件,其中单词数可以从 0 到最多 3 个不等。

我正在使用此代码:

flag_FLAG = 0
for i in range(len(materialfile)):
    if  "FLAG" in materialfile[i] and "=" in materialfile[i]:
        line_FLAG = i
        flag_FLAG = 1
        
    if flag_FLAG == 1:
        
        temp = materialfile[line_FLAG].split(" ")
        for elem in temp:
            if is_word(elem):
                flags = str(elem)

不幸的是,这样我只能得到一个单词(最后一个)。 “is_word”是我创建的一个函数:

def is_word(s):
    try:
        str(s)
        return True
    except ValueError:
        return False

我想将所有单词作为目标。 我希望我已经说清楚了。

I have identified a line in a text file that looks like this:

FLAGS                    = WORD1 WORD2 WORD3

I am reading several files in which the number of words can vary from 0 to a maximum of 3.

I'm using this code:

flag_FLAG = 0
for i in range(len(materialfile)):
    if  "FLAG" in materialfile[i] and "=" in materialfile[i]:
        line_FLAG = i
        flag_FLAG = 1
        
    if flag_FLAG == 1:
        
        temp = materialfile[line_FLAG].split(" ")
        for elem in temp:
            if is_word(elem):
                flags = str(elem)

unfortunately this way I only get one word (the last one).
"is_word" is a function that i creat:

def is_word(s):
    try:
        str(s)
        return True
    except ValueError:
        return False

I would like to get all the words as targets.
I hope I have been clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迟到的我 2025-01-18 16:49:00

我以这种方式解决:

for i in range(len(materialfile)):
    if  "FLAGS" in materialfile[i] and "="  in materialfile[i]:
        line_flag = i
        flag_flag = 1
if flag_flag == 1:
    flags = materialfile[line_flag].split(" = ")[1].split()

我不知道这是否是一种优雅的方式,但它似乎有效。谢谢

I solved in this way:

for i in range(len(materialfile)):
    if  "FLAGS" in materialfile[i] and "="  in materialfile[i]:
        line_flag = i
        flag_flag = 1
if flag_flag == 1:
    flags = materialfile[line_flag].split(" = ")[1].split()

I don't know if this is an elegant way but it seems to work. Thanks

一个人的旅程 2025-01-18 16:49:00

您想要一个嵌套循环,例如:

materialfile = [
    "FLAGS                    = WORD1 WORD2 WORD3",
]

flags = [
    flag
    for line in materialfile if "FLAGS" in line and "=" in line
    for flag in line.split(" = ")[1].split() if flag
]

print(flags)  # ['WORD1', 'WORD2', 'WORD3']

很难说这个确切的代码是否适用于您的实际文件,因为您没有提供示例文件,但希望这能让您指向正确的方向。

请注意,您的 is_word 函数不执行任何操作,因为它们已经是字符串,因此始终会作为无操作转换为 str() 而不会引发异常。上述理解中的 if flag 将过滤掉 flag 中空的值(例如,如果您有像 FLAGS = 这样的行)。

You want a nested loop, e.g.:

materialfile = [
    "FLAGS                    = WORD1 WORD2 WORD3",
]

flags = [
    flag
    for line in materialfile if "FLAGS" in line and "=" in line
    for flag in line.split(" = ")[1].split() if flag
]

print(flags)  # ['WORD1', 'WORD2', 'WORD3']

Hard to say whether this exact code will work with your actual file, since you didn't provide a sample file, but hopefully this gets you pointed in the right direction.

Note that your is_word function does nothing since these are already strings and will hence always convert to str() as a no-op without raising an exception. The if flag in the above comprehension will filter out values of flag that are empty (e.g. if you had a line like FLAGS =).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文