Last active
August 29, 2015 14:07
-
-
Save kelvintaywl/b3490ea7a42c5f2bc363 to your computer and use it in GitHub Desktop.
csv deal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_summary(scores, wafer_num): | |
if wafer_num not in scores: | |
return None | |
wafer_scores = scores[wafer_num] | |
formula = '+'.join(map(str,wafer_scores)) | |
sumScore = sum(wafer_scores) | |
return "For Wafer #%d, the score is %s=%d , the count is %d" % (wafer_num, formula, sumScore, len( wafer_scores)) | |
csv_file = open('YOUR CSV FILE PATH', 'r') | |
scores = {} | |
last_wafer_num = -1 | |
newlines = [] | |
for line in csv_file: | |
arr = line.split(' ') | |
wafer_num = int(arr[1]) | |
wafer_score = int(arr[2]) | |
if last_wafer_num != wafer_num: | |
# we are looking at new set of lines | |
summary = get_summary(scores, last_wafer_num) | |
if summary: | |
newlines.append(summary) | |
if wafer_num not in scores: | |
scores[wafer_num] = [] | |
scores[wafer_num].append(wafer_score) | |
last_wafer_num = wafer_num | |
newlines.append(line) | |
# do last group's summary | |
newlines.append(get_summary(scores, last_wafer_num)) | |
with open('YOUR OUTPUT FILE PATH', 'w+') as outfile: | |
outfile.write('\n'.join(newlines)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment