python3字符串截取_从python3中的字符串中获取特定文本
只是使用
str.split()
和賦值給變量,還有一個(gè)默認(rèn)的第二個(gè)參數(shù)可以幫助您處理唯一的total/free avail場景
def get_free_memory(ssh_obj, raw=True):
stdin, stdout, stderr = ssh_obj.exec_command('free -h')
mem_stats = stdout.readlines()[1]
if raw:
mem_stats_formatted = " ".join(mem_stats.split())
return mem_stats_formatted
else:
_, total, used, free, shared, cache, avail = mem_stats_formatted.split()
return total, avail # You may format this to a string if you wish
編輯:
我不知道你是怎么處理這些信息的,但對我來說,返回一本包含所有數(shù)據(jù)的字典并在以后使用它似乎更有用
def get_free_memory(ssh_obj):
stdin, stdout, stderr = ssh_obj.exec_command('free -h')
mem_stats = stdout.readlines()[1]
_, total, used, free, shared, cache, avail = mem_stats_formatted.split()
memory_info = {
'total': total,
'used': used,
'free': free,
'shared': shared,
'cache': cache,
'available': avail
}
return memory_info
總結(jié)
以上是生活随笔為你收集整理的python3字符串截取_从python3中的字符串中获取特定文本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 笔记本升级显卡一步到位电脑显卡如何升级
- 下一篇: python写520_用Python做一