2015-11-16 25 views
-1

我有以下形式的数据:Python脚本

Overall evaluation: 2 
Invite to interview: 3 
Strength or novelty of the idea (1): 3 
Strength or novelty of the idea (2): 3 
Strength or novelty of the idea (3): 4 
Use or provision of open data (1): 3 
Use or provision of open data (2): 2 
"Open by default" (1): 3 
"Open by default" (2): 2 
Value proposition and potential scale (1): 3 
Value proposition and potential scale (2): 2 
Market opportunity and timing (1): 3 
Market opportunity and timing (2): 2 
Triple bottom line impact (1): 3 
Triple bottom line impact (2): 2 
Triple bottom line impact (3): 3 
Knowledge and skills of the team (1): 2 
Knowledge and skills of the team (2): 4 
Capacity to realise the idea (1): 3 
Capacity to realise the idea (2): 2 
Capacity to realise the idea (3): 3 
Appropriateness of the budget to realise the idea: 2 

我想渲染它,如下所示:

=2+3+3+3+4+3+2+3+2+3+2+3+2+3+2+3+2+4+3+2+3+2 

所以,据我所知,Python是非常强大的这种愚蠢的任务,但我对语法不是很熟悉,那么完成这个目标的最好方法是什么?

也许是这样的:

#create an array 
#array = "=" 
f = open('data.txt', 'r') 
for line in f: 
    #number = grab that number off the end with some kind of regex 
    #array.append(number + "+") 

是否有可能只是在Python shell中运行本程序和数据反馈,然后抓出结果?

+1

如果你知道你的数字总是在行的最后一位是单个数字,那么'line [-1]'会这样做,以提示使用'line.split(“:”)' – shuttle87

+2

'您。否则''re.search('。* \ d +,line)'会做同样的事情。另外,我会建议(如果你打算在第一个总结它)初始化数组为'array = []'然后调用'.append(number)'然后'sum(array)'。一个字符串不能附加上,除非你做'array + = number +'+' –

+0

啊!大!好主意。拉屎。有时我真的很短视。 –

回答

2
with open('data.txt', 'r') as f: 
    for line in f: 
     number = int(line.split(':')[1]) 
     array.append(number) 
print '+'.join(array) 

基本上,使用split函数来获取数字,然后按需要打印它。请注意错误处理以检查它是否为整数,或者如果该行有:或不是

不确定你的意思是shell,但是你可以创建一个函数,然后调用一个函数:

+0

这也可以在shell中工作吗?就像开放它并不断提取数据,将它扔到外壳并收集结果一样? –

+0

在shell中工作是什么意思?在python shell中运行它,还是将它作为shell脚本运行? – roymustang86

+0

我认为他的意思是一个shell脚本 –