python表视图输出
我在output.txt文件中有类似的东西python表视图输出,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,我在output.txt文件中有类似的东西 Service1:Aborted Service2:failed Service3:failed Service4:Aborted Service5:failed 在第二个文件(output2.txt)中输出: 希望以上述表格格式获得输出 我正在尝试的代码: file=open('output.txt','r') target=open('output2.txt','w') states=[] for line in
Service1:Aborted
Service2:failed
Service3:failed
Service4:Aborted
Service5:failed
在第二个文件(output2.txt)中输出:
希望以上述表格格式获得输出
我正在尝试的代码:
file=open('output.txt','r')
target=open('output2.txt','w')
states=[]
for line in file.readlines():
parts=line.strip().split(':')
states.append(parts[1].strip())
target.write(','.join(states))
target.close()
很快:
headers = []
statuses = []
for line in file:
line=line.strip()
parts=line.split(":")
headers.append(parts[0])
statuses.append(parts[1])
format_str = '{:>15}'*(len(headers))
print(format_str.format(*headers))
print(format_str.format(*statuses))
很快:
headers = []
statuses = []
for line in file:
line=line.strip()
parts=line.split(":")
headers.append(parts[0])
statuses.append(parts[1])
format_str = '{:>15}'*(len(headers))
print(format_str.format(*headers))
print(format_str.format(*statuses))
您需要自己去弄清楚如何将输入数据放入numpy数组,以及重塑函数的作用是什么,但这应该能让您完成75%的任务
import numpy as np
import pandas as pd
A_in = [[1,2,3],[4,5,6]]
B = pd.DataFrame(np.array(A_in).reshape(3,2))
B.to_csv('output2.txt,'\t')
您需要自己去弄清楚如何将输入数据放入numpy数组,以及重塑函数的作用是什么,但这应该能让您完成75%的任务
import numpy as np
import pandas as pd
A_in = [[1,2,3],[4,5,6]]
B = pd.DataFrame(np.array(A_in).reshape(3,2))
B.to_csv('output2.txt,'\t')
删除什么逗号??你帖子中的文件没有任何注释对不起,我确实编辑了这个问题@代码直到output.txt和output2.txt中没有逗号为止
var2=part.strip()。不需要旅行!是的,很抱歉造成混淆,我希望得到表格式的输出@Elmovankielmore移动什么逗号??你帖子中的文件没有任何注释对不起,我确实编辑了这个问题@代码直到output.txt和output2.txt中没有逗号为止var2=part.strip()。不需要旅行!是的,很抱歉造成混淆,我希望得到表格式的输出@ElmoVanKielmodo你知道如何删除每个单词之间的空格并添加逗号(,)吗@ADGON32如果要完全删除空格,只需删除格式即可'、'.join(标题)
和'、'.join(状态)
应该适合您@sdrockzz。您知道如何删除每个单词之间的空格并添加逗号(,)吗@ADGON32如果要完全删除空格,只需删除格式即可<代码>'、'.join(标题)
和'、'.join(状态)
应该适用于@sdrockzz。