用于简单信息提取的python脚本

时间:2015-11-16 19:02:13

标签: 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 个答案:

答案 0 :(得分:2)

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

基本上,使用分割功能获取数字,然后根据需要打印。请注意错误处理以检查它是否为整数,或者行是否为:或不是

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