在几个文件中搜索2个变量

发布于 2025-02-04 05:17:50 字数 597 浏览 2 评论 0原文

我正在尝试在几个基于文本的文件中找到2个值。 我拥有的代码是:

def newbat():
query1 = "Dump/data/log/batterystats/newbatterystats*"
name1 = "plug=ac"
strt = "RESET"
dus = os.path.join(path, query1)
entries1 = glob.glob(dus, recursive=True)
for entry in entries1:
    with open(entry, 'r') as file:
        for line in file:
            if name1 in line:
                outs3 = os.path.join(path, 'newbatterystat.txt')
                sys.stdout = open(outs3, 'a')
                print(entry + '\n', line)

name1变量工作。 如果我将name1替换为strt,则可以正常工作。 我找不到如何使用两个变量,因此输出文件是: 重置:时间:01-01-1970插头= AC

I'm trying to find 2 values in several textbased files.
The code i have is:

def newbat():
query1 = "Dump/data/log/batterystats/newbatterystats*"
name1 = "plug=ac"
strt = "RESET"
dus = os.path.join(path, query1)
entries1 = glob.glob(dus, recursive=True)
for entry in entries1:
    with open(entry, 'r') as file:
        for line in file:
            if name1 in line:
                outs3 = os.path.join(path, 'newbatterystat.txt')
                sys.stdout = open(outs3, 'a')
                print(entry + '\n', line)

The name1 variable works.
If I replace name1 to strt it works.
I cannot find out how to use both variables, so the output file is:
RESET:TIME: 01-01-1970 plug=ac

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

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

发布评论

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

评论(3

帅气尐潴 2025-02-11 05:17:50

我确定有很多方法可以解决您的问题,我建议您使用REGEX

>>> import re
>>> text = 'RESET:TIME: 01-01-1970 plug=ac'
>>> re.findall('(RESET|plug=ac)', text)
['RESET', 'plug=ac']

I'm sure there are many ways to solve your problem, I would recommend you to use regex:

>>> import re
>>> text = 'RESET:TIME: 01-01-1970 plug=ac'
>>> re.findall('(RESET|plug=ac)', text)
['RESET', 'plug=ac']
花桑 2025-02-11 05:17:50

真的不需要正则是。

我会为您需要搜索的每个文件做这样的事情。

第一个示例,如果两个单词都必须在线中。第二个示例,如果一个单词的单词必须排成一行。

fruits = """1. orange, pear
2. banana, apple
3. pear, apple
4. apple, banana
5. banana, orange"""

fruits_split = fruits.split('\n')

print('First example:')
for line in fruits_split:
    if 'apple' in line and 'banana' in line:
        print(line)

my_fruits = ['apple', 'banana']
print('\nSecond example:')

for line in fruits_split:
    if any(x in line for x in my_fruits):
        print(line)

输出:

First example:
2. banana, apple
4. apple, banana

Second example:
2. banana, apple
3. pear, apple
4. apple, banana
5. banana, orange

No need for regex really.

I would do something like this for each file you need to search in.

First example if both words has to be in the line. Second example if one word of a list of words has to be in line.

fruits = """1. orange, pear
2. banana, apple
3. pear, apple
4. apple, banana
5. banana, orange"""

fruits_split = fruits.split('\n')

print('First example:')
for line in fruits_split:
    if 'apple' in line and 'banana' in line:
        print(line)

my_fruits = ['apple', 'banana']
print('\nSecond example:')

for line in fruits_split:
    if any(x in line for x in my_fruits):
        print(line)

Output:

First example:
2. banana, apple
4. apple, banana

Second example:
2. banana, apple
3. pear, apple
4. apple, banana
5. banana, orange

吃→可爱长大的 2025-02-11 05:17:50

好吧,我解决了它,但不是最优雅的方式。

def newbat():
    query1 = "Dump/data/log/batterystats/newbatterystats*"
    name1 = 'plug=ac'
    strt = 'RESET:TIME'
    dus = os.path.join(path, query1)
    entries1 = glob.glob(dus, recursive=True)
    for entry in entries1:
        with open(entry, 'r') as file:
            for line in file:
                if name1 in line:
                    outs3 = os.path.join(path, 'newbatterystat.txt')
                    sys.stdout = open(outs3, 'a')
                    print(entry + '\n', line)
                if strt in line:
                    outs3 = os.path.join(path, 'newbatterystat.txt')
                    sys.stdout = open(outs3, 'a')
                    print(entry + '\n', line)

输出是文件名,重置时间和插头= AC。
看来IF在施工中没有比1个变量更高。

Well I solved it, but not the most elegant way.

def newbat():
    query1 = "Dump/data/log/batterystats/newbatterystats*"
    name1 = 'plug=ac'
    strt = 'RESET:TIME'
    dus = os.path.join(path, query1)
    entries1 = glob.glob(dus, recursive=True)
    for entry in entries1:
        with open(entry, 'r') as file:
            for line in file:
                if name1 in line:
                    outs3 = os.path.join(path, 'newbatterystat.txt')
                    sys.stdout = open(outs3, 'a')
                    print(entry + '\n', line)
                if strt in line:
                    outs3 = os.path.join(path, 'newbatterystat.txt')
                    sys.stdout = open(outs3, 'a')
                    print(entry + '\n', line)

The output is filename, reset time and plug=ac.
It appears that the IF IN construction does not take more then 1 variable.

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