将字符串数据转换为列表字符串

时间:2018-08-07 02:04:58

标签: python

示例文件。文本如下:

+++++Host: 192.168.1.1+++++
Port Number: 80
......
product: Apache httpd
IP Address: 192.168.1.1

+++++Host: 192.168.1.2+++++
Port Number: 80
......
product: Apache http
IP Address: 192.168.1.2

+++++Host: 192.168.1.3+++++
Port Number: 80
......
product: Apache httpd
IP Address: 192.168.1.3

+++++Host: 192.168.1.4+++++
Port Number: 80
......
product: Apache httpd
IP Address: 192.168.1.4

+++++Host: 192.168.1.5+++++
Port Number: 80
......
product: Apache httpd
IP Address: 192.168.1.5

-------------------这是我使用的代码---------------------- -------

with open('file.txt', 'r') as f:
    rows = f.read()
    text_lines = rows.split('+++++')
    httpHostList = []
    for i, row in enumerate(text_lines):
        if 'http' in str(row):
            hosts = text_lines[i - 1].strip("Host: ")
            print(httpHostList)

我从httpHostList得到的IP地址很少,如下所示

192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5

要获得以下内容:

[['192.168.1.1'],
['192.168.1.2'],
['192.168.1.3'],
['192.168.1.4'],
['192.168.1.5']]

我尝试了httpHostList.append(hosts),但没有得到想要的结果。

如何将它们转换为预期的格式?

1 个答案:

答案 0 :(得分:0)

似乎您想要包含主机地址的列表列表。尝试在最后一行之后添加

$i = 1;
$newday = array();
if ($current === 'Y') {
    $newday[0] = $datesplit[1];
    do {            
        $sqlstmt = "select * from dbname.mytable_".$newday[$i-1];
        $resultchk = $db->query($sqlstmt);
        $dtset = date($newday[$i-1]);
        $newday[$i] = date('md', strtotime($dtset.' -1 day'));
        $i++;           
    } while(mysqli_num_rows($resultchk) > 0);