Created
January 30, 2017 17:56
-
-
Save psykzz/425251f5dfcafe2eebfaa73e6c008e25 to your computer and use it in GitHub Desktop.
Parse some output from gw2
This file contains hidden or 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 csv | |
STATE = { | |
"BOSS":0, | |
"HEADER":1, | |
"DATA":2, | |
} | |
def main(): | |
results = [] | |
fight = {} | |
current_state = 0 | |
skip_next = False | |
with open('dps.txt') as fh: | |
lines = fh.readlines() | |
for line in lines: | |
if skip_next: | |
skip_next = False | |
continue | |
# reset the state | |
if current_state > 2: | |
current_state = 0 | |
if line[0] == '_': | |
skip_next = True | |
continue # comments | |
line = line.strip() | |
if not line and current_state != STATE['DATA']: | |
continue # empty line at header | |
if current_state == STATE['BOSS']: | |
fight['boss'] = line | |
current_state += 1 | |
continue | |
if current_state == STATE['HEADER']: | |
headers = [l for l in line.split(' ') if l] | |
fight['headers'] = headers | |
current_state += 1 | |
continue | |
if current_state == STATE['DATA']: | |
if 'data' not in fight: | |
fight['data'] = [] | |
details = [l for l in line.split(' ') if l] | |
fight['data'].append(details) | |
if not line and current_state == STATE['DATA']: | |
results.append(fight) | |
fight = {} | |
current_state = 0 | |
skip_next = False | |
with open('output.csv', 'wb') as csvfile: | |
csvw = csv.writer(csvfile, delimiter=',', | |
quotechar='"', quoting=csv.QUOTE_MINIMAL) | |
for r in results: | |
csvw.writerow([r['boss']]) | |
csvw.writerow(r['headers']) | |
for d in r['data']: | |
csvw.writerow(d) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment