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

2024-04-29 01:33:52 发布

您现在位置: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 + "+")

是否可以在pythonshell中运行它并输入数据,然后获取结果?你知道吗


Tags: orandofthetodatalineopen
1条回答
网友
1楼 · 发布于 2024-04-29 01:33:52
with open('data.txt', 'r') as f:
    for line in f:
        number = int(line.split(':')[1])
        array.append(number)
print '+'.join(array)

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

不知道shell是什么意思,但可以创建一个函数,然后调用一个函数:

相关问题 更多 >