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搞定~
python 处理文本 列变成行
答案:3 悬赏:10 手机版
解决时间 2021-03-05 08:04
- 提问者网友:我没有何以琛的痴心不悔
- 2021-03-05 04:05
最佳答案
- 五星知识达人网友:青灯有味
- 2021-03-05 04:35
输入转成字典会把?随便写个横着输出的简单函数。
#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:
不写了,贴过了看不到效果
#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:
不写了,贴过了看不到效果
全部回答
- 1楼网友:神的生死簿
- 2021-03-05 05:06
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'
- 2楼网友:蕴藏春秋
- 2021-03-05 04:56
a = '''ipaddress : 172.22.232.232
scopeid : 172.22.232.0
clientid : 66-55-44-33-22-11
name : test.pc.com
description : test'''
b = a.split('\n')
line1 = ''
line2 = ''
for each in b:
line1 += each.split(':')[0].strip().ljust(20)
line2 += each.split(':')[1].strip().ljust(20)
print line1, '\n', line2
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯