txtsplit.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python
  2. # usage: txtsplit.py <input_file_name> <output_dir>
  3. # splits a txt2gam file into individual location files
  4. # encoded in utf-8, for git to better handle
  5. import os
  6. import sys
  7. import re
  8. import io
  9. assert len(sys.argv) == 3, "usage:\ntxtsplit.py <input_file_name> <output_dir>"
  10. iname = str(sys.argv[1])
  11. odir = str(sys.argv[2])
  12. ifile = io.open(iname, 'rt', encoding='utf-16')
  13. counter = 1
  14. oname = None
  15. firstline = ifile.readline().replace(u'\ufeff','')
  16. match = re.search(ur'^#\s(\w+)$', firstline)
  17. if match:
  18. oname = os.path.join(odir, "%03d_%s" % (counter, match.group(1)))
  19. counter += 1
  20. assert oname, "file is in the wrong format, must start with a location name"
  21. ofile = io.open(oname, 'w', encoding='utf-8')
  22. ofile.write(firstline)
  23. for line in ifile:
  24. match = re.search(ur'^#\s(\w+)$', line)
  25. if match:
  26. ofile.close()
  27. oname = os.path.join(odir, "%03d_%s" % (counter, match.group(1)))
  28. counter += 1
  29. ofile = io.open(oname, 'w', encoding='utf-8')
  30. ofile.write(line)