Created
          March 24, 2017 03:27 
        
      - 
      
- 
        Save myuon/f929b03ab31b40fd74372e75f3061055 to your computer and use it in GitHub Desktop. 
    DSim Ascii parser
  
        
  
    
      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
    
  
  
    
  | #! /usr/bin/env python | |
| #-*- coding:utf-8 -*- | |
| import io | |
| import sys | |
| def transpose(xs): | |
| return list(map(list, zip(*xs))) | |
| def deascii(d): | |
| if d == '-': return 0 | |
| elif d == 'o': return 2 | |
| elif d == '<': return 1 | |
| elif d == '>': return 3 | |
| elif d == 'x': return 4 | |
| elif d == '|': return 0 | |
| else: | |
| print("Cannot interpret: " + d) | |
| return 9 | |
| def main(): | |
| if len(sys.argv) < 2: return | |
| scores = [[]] | |
| with open(sys.argv[1], 'r') as handler: | |
| for i,line in enumerate(handler): | |
| if line[0] == '#': | |
| print(line.strip()) | |
| continue | |
| if line[0] == ';': continue | |
| if line == '\n': | |
| if scores[0] != []: scores.insert(0, []) | |
| continue | |
| if len(line.strip()) != 5: print("line "+str(i)+": lane number must be five") | |
| scores[0].insert(0, line.strip()) | |
| if scores[0] == []: del(scores[0]) | |
| for i,line in enumerate(map(transpose, scores), start=1): | |
| for pos,lane in enumerate(line, start=1): | |
| if all([x == '-' for x in lane]): continue | |
| print("#0,{:03d}:{}:{}".format(i,"".join(map(str, map(deascii, lane))),str(pos)*len(lane))) | |
| if __name__ == "__main__": | |
| main() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment