Skip to content

Instantly share code, notes, and snippets.

@Ramblurr
Created October 31, 2013 10:07
Show Gist options
  • Save Ramblurr/7247268 to your computer and use it in GitHub Desktop.
Save Ramblurr/7247268 to your computer and use it in GitHub Desktop.
Converts quickfort files into single level files for use with dfhack's digfort
#!/usr/bin/python
'''
Converts quickfort files into single level files for use with dfhack's digfort
The main function is to split all multiple level files into single level files,
but it also does some normalization to appease digforts simple parser.
Usage: qf-split-levels.py csv_name new-prefix
Example: qf-split-levels.py mini-moria-01-09.csv mini-moria
__author__ = "Casey Link (unnamedrambler@gmail.com)"
__copyright__ = 'Copyright (c) 2013'
__license__ = 'Public Domain'
'''
import sys
import itertools
import io
def level_split(x):
if x.startswith('#>') or x.startswith('#dig'):
level_split.count += 1
return level_split.count
def normalize(level):
'''level: a list of characters representing a level
returns a normalized string suitable for digfort
'''
level = u''.join(level)
if sys.platform != 'win32':
level.replace('\r\n', '\n')
return level
def main():
if len(sys.argv) != 3:
print("Converts quickfort files into single level csvs for use with dfhack's digfort\n")
print("usage: %s <file_name> level_prefix" % sys.argv[0])
print(" file_name - the csv file to split")
print(" level_prefix - the prefix to give the resulting files\n")
print("Example: %s mini-moria-01-09.csv mini-moria" %sys.argv[0])
print(" This results in 9 files named mini-moria-1.csv - mini-moria-9.csv")
sys.exit(1)
level_split.count = 0
levels = list()
with io.open(sys.argv[1], encoding='UTF-8') as f:
for key,level in itertools.groupby(f, level_split):
levels.append(list(level))
print('Parsed %d levels' % len(levels))
for i,level in enumerate(levels):
i+=1 # 1 index file names
level = map(lambda x: str(x), level)
name = '%s%s.csv' % (sys.argv[2], i)
with io.open(name, 'w+', encoding='UTF-8') as f:
f.write(normalize(level))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment