我有一个与kibana索引相关的字符串列表,字符串如下所示:
λ curl '10.10.43.210:9200/_cat/indices?v'
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open filebeat-2016.10.08 5 1 899 0 913.8kb 913.8kb
yellow open filebeat-2016.10.12 5 1 902 0 763.9kb 763.9kb
yellow open filebeat-2016.10.13 5 1 816 0 588.9kb 588.9kb
yellow open filebeat-2016.10.10 5 1 926 0 684.1kb 684.1kb
yellow open filebeat-2016.10.11 5 1 876 0 615.2kb 615.2kb
yellow open filebeat-2016.10.09 5 1 745 0 610.7kb 610.7kb
日期不分先后地回来了。我想按索引(即日期)对这些文件进行排序filebeat-2016-10.xx ASC或DESC可以。你知道吗
现在,我将字符串隔离如下:
subp = subprocess.Popen(['curl','-XGET' ,'-H', '"Content-Type: application/json"', '10.10.43.210:9200/_cat/indices?v'], stdout=subproce$
curlstdout, curlstderr = subp.communicate()
op = str(curlstdout)
kibanaIndices = op.splitlines()
for index,elem in enumerate(kibanaIndices):
if "kibana" not in kibanaIndices[index]:
print kibanaIndices[index]+"\n"
kibanaIndexList.append(kibanaIndices[index])
但不能以有意义的方式对它们进行排序。你知道吗
这是你需要的吗?你知道吗
这里
extract_date
是一个返回第三列的函数(如filebeat-2016.10.12
)。我们将此函数用作key
参数,以sort
将此值用作排序键。日期格式可以按字符串排序。您可能可以使用更复杂的extract_line
函数来仅提取日期。你知道吗我将您的示例数据作为UTF-8复制到一个文本文件中,因为我无法访问您引用的服务器。使用列表理解和字符串方法,您可以清理数据,然后将其分解为各个组成部分。排序是通过将lambda函数作为参数传递给内置sorted()方法完成的:
更多关于列表理解的信息:https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
有关排序的详细信息:https://wiki.python.org/moin/HowTo/Sorting
相关问题 更多 >
编程相关推荐