Skip to content

Instantly share code, notes, and snippets.

@fraxnet
Forked from tfrisk-old/paradox-csv-converter.py
Created September 18, 2019 15:27
Show Gist options
  • Select an option

  • Save fraxnet/ca38e87b4dcaab004de0e7c4d3fc8009 to your computer and use it in GitHub Desktop.

Select an option

Save fraxnet/ca38e87b4dcaab004de0e7c4d3fc8009 to your computer and use it in GitHub Desktop.
Paradox database -> CSV converter
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Paradox database -> CSV converter
# (C) 2016 Teemu Frisk / Pandia Ltd
# MIT License
#
# Usage:
# 1) Make sure this script and its related paradox.py files are in the same dir
# 2) Create directory for paradox files and copy the entire db there
# NOTE: Directory name will be appended to the generated csv file names
# 3) Run this script
# Example: $ python2 ./paradox-csv-converter.py
# NOTE: Python v2 is required, paradox.py won't work with Python 3
# 4) Enjoy your paradox database converted to CSV
# NOTE: Empty tables are ignored
# NOTE: Generated files may have encoding issues depending on source data
# paradox.py should be located in the same directory than this file
# Original source: https://gist.github.com/BertrandBordage/9892556
import paradox
import fnmatch
from os import path, walk, mkdir
from re import sub
from sys import exit, stdout
DEBUG=False # Show debug messages? True of False
def form_file_name(inputfile):
line = sub('\.\/','', inputfile) # Remove leading ''./'
line = sub(r'[\s\/]','_', line) # Convert special chars to '_' (spaces and '/')
return line.lower()
matches = []
# Walk current directory recursively and list all '.DB' files
# Original source: http://stackoverflow.com/a/2186565
for root, dirnames, filenames in walk('.'):
for filename in fnmatch.filter(filenames, '*.DB'):
matches.append(path.join(root, filename))
if not path.exists('./output/'):
mkdir('output')
all_files = len(matches)
csv_files = 0
ign_files = 0
for dbfile in matches:
outputfile = path.join('./output/' + form_file_name(dbfile) + '.csv')
if DEBUG:
print('dbfile: ' + dbfile)
print('outputfile: ' + outputfile)
try:
paradox.to_csv(dbfile, outputfile)
if not DEBUG:
stdout.write('.') # Print some progress indicator
csv_files += 1
except Exception as e:
if DEBUG:
print('Ignoring empty file [' + dbfile + ']')
ign_files += 1
print('\nParadox database processed, output csv files located in ./output/ directory.')
print('Total input files: ' + str(all_files))
print('Csv files generated: ' + str(csv_files))
print('Ignored: ' + str(ign_files))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment