입력하다:
{"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
답변1
스크립트를 일부 수정합니다.
import sys, re, ast
with open(sys.argv[1], 'r') as f:
items = re.findall(r'\{.+?\}', re.sub(r"(\d+),(\d+)", "\\1.\\2,", f.read().replace('\n','')))
items = [ast.literal_eval(i) for i in items]
sum = 0
d_qty = 0
for d in items:
if d['emailed'] == 'yes' and d['accepted'] == 'no':
sum += d['vote']
d_qty += 1
print(d)
print('Lines Before: {}'.format(len(items)))
print('Lines After: {}'.format(d_qty))
print(sum)