入力する:
{"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)