免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2370 | 回复: 2
打印 上一主题 下一主题

数据格式转换求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-11-12 15:10 |只看该作者 |倒序浏览
| net.if.in[eth0]                    | hh-yun-compute-131092.vclound.com |                6592 |
| net.if.in[eth1]                    | hh-yun-compute-131092.vclound.com |                  112 |
| net.if.out[eth0]                  | hh-yun-compute-131092.vclound.com |                5512 |
| net.if.out[eth1]                  | hh-yun-compute-131092.vclound.com |                     0 |
| vm.memory.size[available] | hh-yun-compute-131092.vclound.com | 134131691520 |
| net.if.in[eth0]                    | hh-yun-compute-131093.vclound.com |               6840 |
| net.if.in[eth1]                    | hh-yun-compute-131093.vclound.com |                   24 |
| net.if.out[eth0]                  | hh-yun-compute-131093.vclound.com |               5552 |
| net.if.out[eth1]                  | hh-yun-compute-131093.vclound.com |                     0 |
| vm.memory.size[available] | hh-yun-compute-131093.vclound.com | 134133485568 |
| net.if.in[eth0]                    | hh-yun-compute-131094.vclound.com |               6608 |
| net.if.in[eth1]                    | hh-yun-compute-131094.vclound.com |                   88 |
| net.if.out[eth0]                  | hh-yun-compute-131094.vclound.com |               5440 |
| net.if.out[eth1]                  | hh-yun-compute-131094.vclound.com |                     0 |
| vm.memory.size[available] | hh-yun-compute-131094.vclound.com | 134134104064 |
| net.if.in[eth0]                    | hh-yun-compute-131095.vclound.com |                6656 |
| net.if.in[eth1]                    | hh-yun-compute-131095.vclound.com |                   48 |
| net.if.out[eth0]                  | hh-yun-compute-131095.vclound.com |                5440 |
| net.if.out[eth1]                  | hh-yun-compute-131095.vclound.com |                     0 |
| vm.memory.size[available] | hh-yun-compute-131095.vclound.com | 134136803328 |
+-------------------------------+-------------------------------------------+-----------------+


从数据中获取上面的内容后,  我是否可以把他们转换成为 json 格式?


data:{'hh-yun-compute-131092.vclound.com':{'net.if.in[eth0]':'6592','net.if.in[eth1]':'112','net.if.out[eth0]':'5512','net.if.out[eth1]':'0','vm.memory.size[available] ':'134131691520'}, hh-yun-compute-131093.vclound.com:{'net.if.in[eth0]':'6592','net.if.in[eth1]':'112','net.if.out[eth0]':'5512','net.if.out[eth1]':'0','vm.memory.size[available] ':'134131691520'} ........}

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
2 [报告]
发表于 2014-11-12 18:44 |只看该作者
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-

  3. import json, fileinput, sys, re
  4. if len(sys.argv) != 2:
  5.     print "usage: %s [urfile]" % sys.argv[0]
  6.     sys.exit(1)

  7. d = dict()
  8. for line in fileinput.input(sys.argv[1]):
  9.     l = [ r.strip() for r in line.strip().split("|")]
  10.     if not d.has_key(l[2]): d[l[2]] = dict()
  11.     d[l[2]][l[1]] = l[3]
  12. print "data:"+json.dumps(d)
复制代码

论坛徽章:
0
3 [报告]
发表于 2014-12-29 10:49 |只看该作者
感谢, 使用 JSON_DECODE 更加简单
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP