python中横向制表符_如何检查列表中有制表符的Python?

我有一个带belwo内容的data.csv文件,该文件的末尾也有一些新行。现在,我想读取此文件并从特定列的最后一行获取值。

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 | --- |

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

现在,我想解析以上文件和最后一行的额外值。我想在最后一行的“客户ID”和“信任域”列中增加附加值:

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块行出现此错误:

Traceback (most recent call last):

File "test.py", line 11, in

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

IndexError: list index out of range

这是最后一行打印出来的行:

[' \t\t']

解决方案

您可以尝试将每行与分割|成字典列表,然后仅从最后一行打印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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值