输入:
{"case":"0901","emailed":"yes","vote":1,"accepted":"no"},
{"case":"0908","emailed":"yes","vote":8,1"accepted":"yes"},
{"case":"0911","emailed":"no","vote":10,1"accepted":"yes"},
{"case":"0090","emailed":"yes","vote":3,1"accepted":"no"},
编写脚本以接受输入,即使它类似于以下内容:
{"case":"0901","emailed":"yes","vote":1,"accepted":"no"},{"case":"0908","emailed":"yes","vote":8,1"accepted":"yes"},{"case":"0911","emailed":"no","vote":10,1"accepted":"yes"},{"case":"0090","emailed":"yes","vote":3,1"accepted":"no"},
输出
{'case': '0901', 'vote': 1, 'accepted': 'no', 'emailed': 'yes'}
{'case': '0090', 'vote': 3.1, 'accepted': 'no', 'emailed': 'yes'}
Lines Before: 4
Lines After: 2
4.1
我的剧本
import sys, re, ast
with open(sys.argv[1], 'r') as f:
items = ast.literal_eval(re.sub(r"(\d+),(\d+)", "\\1.\\2,", f.read().replace('\n','')))
sum = 0
for d in items:
if d['emailed'] == 'yes' and d['accepted'] == 'no':
sum += d['vote']
print(d)
print(sum)
所以我正在寻找的是如何编辑我的脚本以计算提供给脚本的行数before
和after
!
对脚本稍作修改: