2015-11-16 45 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腳本 –