python 处理文本 列变成行
IPAddress:172.22.232.232ScopeId:172.22.232.0ClientId:66-55-44-33-22-11Name:test.pc.co...
IPAddress : 172.22.232.232
ScopeId : 172.22.232.0
ClientId : 66-55-44-33-22-11
Name : test.pc.com
Description : test
变成
IPAddress ScopeId ClientId Name Description
172.22.232.232 172.22.232.0 66-55-44-33-22-11 test.pc.com test
求大神用python搞定~ 展开
ScopeId : 172.22.232.0
ClientId : 66-55-44-33-22-11
Name : test.pc.com
Description : test
变成
IPAddress ScopeId ClientId Name Description
172.22.232.232 172.22.232.0 66-55-44-33-22-11 test.pc.com test
求大神用python搞定~ 展开
2个回答
展开全部
输入转成字典会把?随便写个横着输出的简单函数。
#encoding=utf-8
#!/usr/bin/env python
def pretty_print(d):
#判断最长的一个大概占多少位
max_len = max(map(len, d.keys() + d.values()))
print "max len: %s"%max_len
#输出
head_str = ""
value_str = ""
for k in d:
v = d.get(k,"")
head_str += "%s "%k + " "*(max_len - len(k))
value_str += "%s "%d.get(k,"") + " "*(max_len - len(d.get(k,"")))
print "*"*max_len*len(d)
print head_str
print value_str
print "*"*max_len*len(d)
data = {
"IPAddress" : "172.22.232.232",
"ScopeId" : "172.22.232.0",
"ClientId" : "66-55-44-33-22-11",
"Name" : "test.pc.com",
"Description" : "test",
}
pretty_print(data)
#Output:
不写了,贴过了看不到效果
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Python 3.6.1 (default, Sep 7 2017, 16:36:03)
[GCC 6.3.0 20170406] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> s = """
... IPAddress : 172.22.232.232
... ScopeId : 172.22.232.0
... ClientId : 66-55-44-33-22-11
... Name : test.pc.com
... Description : test
... """
>>> ss = s.strip().split('\n')
>>> ss
['IPAddress : 172.22.232.232', 'ScopeId : 172.22.232.0', 'ClientId : 66-55-44-33-22-11', 'Name : test.pc.com', 'Description : test']
>>> sss = [i.split(':') for i in ss]
>>> sss
[['IPAddress ', ' 172.22.232.232'], ['ScopeId ', ' 172.22.232.0'], ['ClientId ', ' 66-55-44-33-22-11'], ['Name ', ' test.pc.com'], ['Description ', ' test']]
>>> line1 = ' '.join([i[0].strip() for i in sss])
>>> line1
'IPAddress ScopeId ClientId Name Description'
>>> line2 = ' '.join([i[1].strip() for i in sss])
>>> line2
'172.22.232.232 172.22.232.0 66-55-44-33-22-11 test.pc.com test'
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询