Example #1
0
    def test_convert_json(self):
        args = ['examples/testjson.json']
        output_file = six.StringIO()

        utility = In2CSV(args, output_file)
        utility.main()

        target_output = open('examples/testjson_converted.csv', 'r').read()
        self.assertEqual(output_file.getvalue(), target_output)
Example #2
0
    def test_convert_specific_xls_sheet(self):
        args = ['-f', 'xls', '--sheet', 'data', 'examples/sheets.xls']
        output_file = six.StringIO()

        utility = In2CSV(args, output_file)
        utility.main()

        target_output = open('examples/testxls_converted.csv', 'r').read()
        self.assertEqual(output_file.getvalue(), target_output)
Example #3
0
    def test_convert_xls(self):
        args = ['-f', 'xls', 'examples/test.xls']
        output_file = StringIO.StringIO()

        utility = In2CSV(args, output_file)
        utility.main()

        target_output = open('examples/testxls_converted.csv', 'r').read()
        self.assertEqual(output_file.getvalue(), target_output)
Example #4
0
    def test_csv_no_headers(self):
        args = ['--no-header-row', 'examples/no_header_row.csv']
        output_file = six.StringIO()

        utility = In2CSV(args, output_file)
        utility.main()

        output = output_file.getvalue()

        self.assertTrue('A,B,C' in output)
Example #5
0
def ensure_csv(filename):
    """Ensure that `filename` is a CSV.

    :param filename: Name of tabular file
    :returns: File pointer to original or converted file
    """
    _, ext = os.path.splitext(filename)
    if ext == '.csv':
        return open(filename)
    logger.info('Converting file {0} to CSV'.format(filename))
    file = tempfile.NamedTemporaryFile('w')
    converter = In2CSV((filename, ))
    converter.args.input_path = filename
    converter.output_file = file
    converter.main()
    return file
Example #6
0
def csvify(file_path=None,add_args=None):
    """
    wrapper to convert
    spreadsheet to csv file
    with optional args
    """
    # naming stuff
    file_dir = '/'.join(file_path.split('/')[:-1]) + '/'
    file_name = file_path.split('/')[-1]
    file_ext = file_name.split('.')[-1]
    #output_file_path = file_dir + file_name.replace(file_ext,'csv')
    output_file_path = file_name.replace(file_ext,'csv')
    output_file = open(output_file_path,'w')
    
    # conversion
    args = [file_path,'--format='+file_ext,'--no-inference']
    if add_args:
        args = args + add_args
    util = In2CSV(args=args,output_file=output_file)
    util.main()
    output_file.close()
    return output_file_path