How to use the csvkit.convert.guess_format function in csvkit

To help you get started, we’ve selected a few csvkit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wireservice / csvkit / tests / test_convert / test_convert.py View on Github external
def test_guess_json(self):
        self.assertEqual('json', convert.guess_format('testdata.json'))
github wireservice / csvkit / tests / test_convert / test_convert.py View on Github external
def test_guess_csv(self):
        self.assertEqual('csv', convert.guess_format('testdata.csv'))
github wireservice / csvkit / tests / test_convert / test_convert.py View on Github external
def test_guess_invalid(self):
        self.assertEqual(None, convert.guess_format('testdata.invalid'))
github wireservice / csvkit / tests / test_convert.py View on Github external
def test_xlsx(self):
        format = convert.guess_format('testdata.xlsx')
        self.assertEqual(format, 'xlsx')
github wireservice / csvkit / tests / test_convert / test_convert.py View on Github external
def test_guess_fixed(self):
        self.assertEqual('fixed', convert.guess_format('testdata'))
github wireservice / csvkit / tests / test_convert.py View on Github external
def test_fixed(self):
        format = convert.guess_format('testdata')
        self.assertEqual(format, 'fixed')
github wireservice / csvkit / tests / test_convert / test_convert.py View on Github external
def test_guess_xlsx(self):
        self.assertEqual('xlsx', convert.guess_format('testdata.xlsx'))
github associatedpress / geomancer / geomancer / views.py View on Github external
def upload():
    context = {}
    if request.method == 'POST':
        f = request.files['input_file']
        if f:
            if allowed_file(f.filename):
                inp = StringIO(f.read())
                if sys.getsizeof(inp.getvalue()) <= MAX_CONTENT_LENGTH:
                    inp.seek(0)
                    file_format = convert.guess_format(f.filename)
                    try:
                        converted = convert.convert(inp, file_format)
                    except UnicodeDecodeError:
                        context['errors'] = ['We had a problem with reading your file. \
                            This could have to do with the file encoding or format']
                        converted = None
                    f.seek(0)
                    if converted:
                        outp = StringIO(converted)
                        reader = UnicodeCSVReader(outp)
                        session['header_row'] = reader.next()
                        rows = []
                        columns = [[] for c in session['header_row']]
                        column_ids = range(len(session['header_row']))
                        for row in range(100):
                            try:
github wireservice / csvkit / csvkit / utilities / in2csv.py View on Github external
def main(self):
        path = self.args.input_path

        # Determine the file type.
        if self.args.filetype:
            filetype = self.args.filetype
            if filetype not in SUPPORTED_FORMATS:
                self.argparser.error('"%s" is not a supported format' % self.args.filetype)
        elif self.args.schema:
            filetype = 'fixed'
        elif self.args.key:
            filetype = 'json'
        else:
            if not path or path == '-':
                self.argparser.error('You must specify a format when providing input as piped data via STDIN.')
            filetype = convert.guess_format(path)
            if not filetype:
                self.argparser.error('Unable to automatically determine the format of the input file. Try specifying a format with --format.')

        if self.args.names_only:
            if filetype in ('xls', 'xlsx'):
                sheets = self.sheet_names(path, filetype)
                for sheet in sheets:
                    self.output_file.write('%s\n' % sheet)
            else:
                self.argparser.error('You cannot use the -n or --names options with non-Excel files.')
            return

        # Set the input file.
        if filetype in ('xls', 'xlsx'):
            self.input_file = self.open_excel_input_file(path)
        else: