Last active
August 29, 2015 14:01
-
-
Save bemasher/12d41a328a3573fd99fa to your computer and use it in GitHub Desktop.
For all *.txt and *.awd files in the data folder, calculate the sum of each line across all files starting at the 20th value.
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
import glob | |
output = [] | |
# For both extensions | |
for ext in ["txt", "awd"]: | |
# For all files matching the current extension | |
for filename in glob.iglob("data/*.{}".format(ext)): | |
print filename | |
# For each line in current file | |
for idx, line in enumerate(open(filename)): | |
# Skip first 19 lines | |
if idx < 19: | |
continue | |
# Array offset (arrays are 0-indexed) | |
offset = idx - 19 | |
# If the output array isn't long enough, append an element | |
if len(output) <= offset: | |
output.append(0) | |
# Add current value to the output array at the current offset | |
output[offset] += int(line) | |
# Create the output file | |
with open("sums.txt", "w") as sums: | |
# Write all sums, one per line | |
for val in output: | |
print >>sums, val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment