如何在Python中返回整个字符串并从中提取列? [关闭]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Python中返回整个字符串并从中提取列? [关闭]相关的知识,希望对你有一定的参考价值。
例如,从这个输出中,我需要带有单词'test1.txt'的字符串,然后我需要该字符串中的第三列,即文件大小。像Linux中的“cut”命令
5636335 -rw- 1922 Apr 20 2019 09:22:47 +00:00 private-config.cfg
5636332 -rw- 1136 Apr 20 2019 09:22:47 +00:00 NETMAP
5636336 -rw- 0 Apr 20 2019 13:14:51 +00:00 test1.txt
5636325 -rw- 1691 Apr 20 2019 09:22:47 +00:00 startup-config.cfg
5636333 -rw- 16384 Apr 20 2019 09:22:47 +00:00 nvram_00001
5636330 -rw- 341 Apr 20 2019 09:22:47 +00:00 ubridge.log
NETMIKO module
net_connect = ConnectHandler(**cisco)
output = net_connect.send_command('dir')
x = re.search('test1.txt', output)
print(x)
<re.Match object; span=(215, 224), match='test1.txt'>
答案
你可以切片[13:25]
和strip()
output = '''5636335 -rw- 1922 Apr 20 2019 09:22:47 +00:00 private-config.cfg
5636332 -rw- 1136 Apr 20 2019 09:22:47 +00:00 NETMAP
5636336 -rw- 0 Apr 20 2019 13:14:51 +00:00 test1.txt
5636325 -rw- 1691 Apr 20 2019 09:22:47 +00:00 startup-config.cfg
5636333 -rw- 16384 Apr 20 2019 09:22:47 +00:00 nvram_00001
5636330 -rw- 341 Apr 20 2019 09:22:47 +00:00 ubridge.log'''
for row in output.split('\n'):
if 'test1.txt' in row:
print(row[13:25].strip())
列之间有很多空格,所以正常的split(' ')
会创建太多的空列,它可能不起作用,但re.split("\s+")
可以做到这一点
output = '''5636335 -rw- 1922 Apr 20 2019 09:22:47 +00:00 private-config.cfg
5636332 -rw- 1136 Apr 20 2019 09:22:47 +00:00 NETMAP
5636336 -rw- 0 Apr 20 2019 13:14:51 +00:00 test1.txt
5636325 -rw- 1691 Apr 20 2019 09:22:47 +00:00 startup-config.cfg
5636333 -rw- 16384 Apr 20 2019 09:22:47 +00:00 nvram_00001
5636330 -rw- 341 Apr 20 2019 09:22:47 +00:00 ubridge.log'''
import re
for row in output.split('\n'):
if 'test1.txt' in row:
print(re.split('\s+', row)[2])
另一答案
您可以使用:
tr -s ' ' <test1.txt | cut -d ' ' -f3
1922
1136
0
1691
16384
341
ts -s | squeeze-repeats
cut -d | delimiter
cut -f | field
我知道如何在Linux中完成它,我需要Python帮助
import re
sizes = [re.split(r"\s+", l)[2] for l in open("test1.txt").readlines()]
# ['1922', '1136', '0', '1691', '16384', '341']
以上是关于如何在Python中返回整个字符串并从中提取列? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章