代码之家  ›  专栏  ›  技术社区  ›  flash

如何检查python中包含制表符的列表?

  •  0
  • flash  · 技术社区  · 6 年前

    Connecting to the ControlService endpoint
    
    Found 3 rows.
    Requests List:
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     Client ID                                                                   | Client Type                  | Service Type | Status               | Trust Domain              | Data Instance Name | Data Version | Creation Time              | Last Update                | Scheduled Time | 
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     REFRESH_ROUTINGTIER_ARTIFACTS_1465901168866                              | ROUTINGTIER_ARTIFACTS | SYSTEM       | COMPLETED            | RRA Bulk Client    | soa_server1       | 18.2.2.0.0  | 2016-06-14 03:49:55 -07:00 | 2016-06-14 03:49:57 -07:00 | ---            | 
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     500333443                                                          | CREATE                        | [FA_GSI]     | COMPLETED            | holder       | soa_server1       | 18.3.2.0.0  | 2018-08-07 11:59:57 -07:00 | 2018-08-07 12:04:37 -07:00 | ---            | 
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     500333446                                                          | CREATE                        | [FA_GSI]     | COMPLETED            | holder-test  | soa_server1       | 18.3.2.0.0  | 2018-08-07 12:04:48 -07:00 | 2018-08-07 12:08:52 -07:00 | ---            | 
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    

    现在我要分析上面的文件和最后一行的额外值。我想在最后一行增加“Client ID”和“Trust Domain”列的值,即:

    Client ID: 500333446
    Trust Domain: holder-test
    

    我得到了下面的python脚本,但是由于csv文件末尾的新行而失败了?如果我的csv文件没有任何新行,那么它可以正常工作。

    import csv
    
    lines_to_skip = 4
    with open('data.csv', 'r') as f:
        reader = csv.reader(f, delimiter='|')
        for i in range(lines_to_skip):
            next(reader)
    
        data = []
        for line in reader:
            if line[0].find("---") != 0:
                print line
                data.append(line)
    
    print("{}={}".format(data[-1][0].replace(" ",""),data[-1][4].replace(" ","")))
    

    如果csv文件的末尾有一些新行,则在if block line处会出现此错误:

    Traceback (most recent call last):
      File "test.py", line 11, in <module>
        if line[0].find("---") != 0:
    IndexError: list index out of range
    

    这是最后打印出来的行:

    [' \t\t']
    
    3 回复  |  直到 6 年前
        1
  •  2
  •   RoadRunner    6 年前

    你可以试着用 | 在字典列表中只打印 Client ID Trust Domain 从最后一行:

    with open('data.txt') as f:
    
        # collect rows of interest
        rows = []
        for line in f:
            if '|' in line:
                items = [item.strip() for item in line.split('|')]
                rows.append(items)
    
        # first item will be headers
        headers = rows[0]
    
        # put each row into dictionary
        data = [dict(zip(headers, row)) for row in rows[1:]]
    
        # print out last row information of interest
        print('Client ID:', data[-1]['Client ID'])
        print('Trust Domain:', data[-1]['Trust Domain'])
    

    哪些输出:

    Client ID: 500333446
    Trust Domain: holder-test
    

    500333446=holder-test 相反,您可以将最终打印顺序更改为:

    print('%s=%s' % (data[-1]['Client ID'], data[-1]['Trust Domain']))
    # 500333446=holder-test
    
        2
  •  1
  •   abarnert    6 年前

    如果结尾有空行 csv.reader line[0] 在每一行,即使是空的,你都会得到你所要求的例外。

    但你只要检查一下 line 在尝试检查之前为空 :

    if line:
        if line[0].find("---") != 0:
    

    或者,更简洁地说:

    if line and line[0].find("---") != 0:
    
        3
  •  -1
  •   Moon Cheesez    6 年前

    在处理该行之前,您应该 strip 关闭任何不需要的字符,并验证它是您想要的行。

    if line and line[0].strip(" \t") and not line[0].startswith("---"):
    

    或者另一种方式:

    if all([line, line[0].strip(" \t"), not line[0].startswith("---")]):
    
    1. if line 检查是否 line
    2. line[0].strip(" \t") 检查第一个值是否只包含不需要的字符。
    3. not line[0].startswith("---") line[0].find("---") != 0