├── README.md ├── LICENSE └── dta2csv.py /README.md: -------------------------------------------------------------------------------- 1 | dta2csv is a Python3 script to covert a World Band dta-formatted data file to acsv-formatted file. Using it will overwrite an existing csv file. 2 | 3 | Usage: dta2csv.py -f input_file_name (without the .dta extension) 4 | 5 | Example for converting data.dta to data.csv: 6 | dta2csv.py -f data 7 | 8 | Python3 library dependencies: argparse, os and pandas 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Karl Kirschner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /dta2csv.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # World Bank's DTA data conversion to csv file 4 | # Author: Karl Kirschner, 2019 5 | 6 | import argparse 7 | import os 8 | import pandas as pd 9 | 10 | 11 | def convert_data(basename=None): 12 | 13 | try: 14 | os.path.exists('{0}.dta'.format(basename)) 15 | print() 16 | print('Converting {0}.dta to {0}.csv.'.format(basename)) 17 | except FileNotFoundError: 18 | print() 19 | print('The file ({0}.dta) does not exist.'.format(basename)) 20 | 21 | data = pd.io.stata.read_stata('{0}.dta'.format(basename)) 22 | data.to_csv('{0}.csv'.format(basename)) 23 | 24 | 25 | if __name__ == "__main__": 26 | """ 27 | Usage: dta2csv.py -f input_file_name (without the .dta extension) 28 | 29 | Example for converting data.dta to data.csv: ./dta2csv.py -f data 30 | 31 | Note that this will overwrite an existing csv file. 32 | """ 33 | 34 | parser = argparse.ArgumentParser() 35 | parser.add_argument("-f", "--inputfile", type=str, help="Basename for DTA input data file.", action="store") 36 | args = parser.parse_args() 37 | convert_data(basename=args.inputfile) 38 | --------------------------------------------------------------------------------