Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python如何查找特定字符串并将其添加到列表中_Python_Python 3.x_Arraylist - Fatal编程技术网

Python如何查找特定字符串并将其添加到列表中

Python如何查找特定字符串并将其添加到列表中,python,python-3.x,arraylist,Python,Python 3.x,Arraylist,如何将文本文件中的特定字符串保存到数组/列表中 Textfile.txt: ... Certificate Name: domain1.com Domains: domain1.com Expiry Date: 2020-08-17 17:56:08+00:00 (VALID: 41 days) Certificate Path: /etc/letsencrypt/live/domain1.com/fullchain.pem Private Key Path:

如何将文本文件中的特定字符串保存到数组/列表中

Textfile.txt:

...
  Certificate Name: domain1.com
    Domains: domain1.com
    Expiry Date: 2020-08-17 17:56:08+00:00 (VALID: 41 days)
    Certificate Path: /etc/letsencrypt/live/domain1.com/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/domain1.com/privkey.pem
  Certificate Name: domain2.com
    Domains: domain2.com
    Expiry Date: 2020-08-17 17:56:08+00:00 (VALID: 31 days)
    Certificate Path: /etc/letsencrypt/live/domain2.com/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/domain2.com/privkey.pem
  Certificate Name: domain3.com
    Domains: domain3.com
    Expiry Date: 2020-08-17 17:56:08+00:00 (VALID: 13 days)
    Certificate Path: /etc/letsencrypt/live/domain3.com/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/domain3.com/privkey.pem
...
现在的问题是如何将证书名:放入Python中的一个列表中,而仅此列表

当我使用grep-oP'?时,使用Bash很容易,下面是如何使用str.split:

输出:

['domain1.com', 'domain2.com', 'domain3.com']
Certificate Name: domain1.com
Certificate Name: domain2.com
Certificate Name: domain3.com

['Certificate Name: domain1.com',
 'Certificate Name: domain2.com',
 'Certificate Name: domain3.com']

这提供了一个更整洁的输出,应该允许您更好地理解代码!祝你好运

# Create List
completeList = []

# Make Python Open File in READ MODE...
with open("Textfile.txt", "r") as source_file:
    for line in source_file:
        stripped_line = line.strip()
        # If the current line contains the string below...
        if stripped_line.count("Certificate Name") != 0:
            # Add to List!
            completeList.append(stripped_line)
        else:
            continue

# Print each certificate name separately...
for item in completeList:
    print(item)
输出:

['domain1.com', 'domain2.com', 'domain3.com']
Certificate Name: domain1.com
Certificate Name: domain2.com
Certificate Name: domain3.com

['Certificate Name: domain1.com',
 'Certificate Name: domain2.com',
 'Certificate Name: domain3.com']

我宁愿选择正则表达式,也不愿逐行循环

import re
 
with open('file.txt', 'r') as f:
    txt = f.read()

print(re.findall("(Certificate Name: .*.com)", txt))
输出:

['domain1.com', 'domain2.com', 'domain3.com']
Certificate Name: domain1.com
Certificate Name: domain2.com
Certificate Name: domain3.com

['Certificate Name: domain1.com',
 'Certificate Name: domain2.com',
 'Certificate Name: domain3.com']

我会使用比s和l更显式的变量名,这让我自然而然地想到一个字符串和一个列表。也许是台词和台词?哇,看起来很简单。谢谢