Convenience function, as :func:`petl.io.csv.teecsv` but with different
    default dialect (tab delimited).
175066
def teetsv(table, source=None, encoding=None, errors='strict', write_header=True,
           **csvargs):
    

    csvargs.setdefault('dialect', 'excel-tab')
    return teecsv(table, source=source, encoding=encoding, errors=errors,
                  write_header=write_header, **csvargs)
1259285
def read_csv_with_different_type(csv_name, column_types_dict, usecols=None):
    
    csv_path = os.path.join(DATA_FOLDER, csv_name)
    csv = pd.read_csv(
        csv_path,
        usecols=usecols,
        encoding="utf-8",
        dtype=column_types_dict,
        engine="python",
    )

    for key_column, val_type in column_types_dict.items():
        if val_type == str:
            csv[key_column] = csv[key_column].str.strip()

    return csv
654545
function(lines, param) {
              var csvLines = [];
              var left = "";
              while (lines.length) {
                var line = left + lines.shift();
                var row = rowSplit(line, param);
                if (row.closed) {
                  csvLines.push(row.cols);
                  left = "";
                } else {
                  left = line + (getEol(line, param) || "\n"); // if unable to getEol from data, assume "\n"
                }
              }
              return {lines: csvLines, partial: left};
            }
133552
def load_csv_to_model(path, model, field_names=None, delimiter=None, batch_len=10000, 
                      dialect=None, num_header_rows=1, mode='rUb',
                      strip=True, clear=False, dry_run=True, ignore_errors=True, verbosity=2):
    ''''''

    reader_kwargs = {}
    errors = collections.Counter()
    if delimiter or dialect:
        reader_kwargs['dialect'] = dialect or 'excel'
        if delimiter:
            reader_kwargs['delimiter'] = delimiter
    reader_kwargs['delimiter'] = str(reader_kwargs['delimiter'][0])
    delimiter = reader_kwargs['delimiter']

    path = path or './'
    if not delimiter:
        for d in ',', '|', '\t', ';':
            try:
                return load_csv_to_model(path=path, model=model, field_names=field_names, delimiter=d, batch_len=batch_len,
                                         dialect=dialect, num_header_rows=num_header_rows,
                                         strip=strip, clear=clear, dry_run=dry_run, ignore_errors=ignore_errors, verbosity=verbosity)
            except:
                pass
        return None

    if clear:
        clear_model(model, dry_run=dry_run, verbosity=verbosity)

    M = 0
    with open(path, mode) as f:
        reader = csv.reader(f, **reader_kwargs)
        header_rows = []
        i = 0
        while len(header_rows) < num_header_rows and i < 100:
            row = reader.next()
            i += 1
            if not row or any(compiled_regex.match(row[0]) for compiled_regex in header_rows_to_ignore):
                if verbosity > 1:
                    print 'IGNORED: %r' % row
            else:
                header_rows += [row]
        if verbosity > 2:
            print 'HEADER: %r' % header_rows
        if verbosity > 0:
            N = count_lines(path, mode) - i + 10  # + 10 fudge factor in case multiple newlines in a single csv row
            widgets = [pb.Counter(), '/%d lines: ' % N, pb.Percentage(), ' ', pb.RotatingMarker(), ' ', pb.Bar(),' ', pb.ETA()]
            i, pbar = 0, pb.ProgressBar(widgets=widgets, maxval=N).start()
        if verbosity > 3:
            print 'Generating all the batches before iterating may take a while...'
        for batch_num, batch_of_rows in enumerate(util.generate_batches(reader, batch_len)):
            if verbosity > 2:
                print 
                print i
            batch_of_objects = []
            for j, row in enumerate(batch_of_rows):
                if verbosity > 3:
                    print j, row
                if not row or all(not el for el in row):
                    if verbosity > 2:
                        print 'IGNORED: %r' % row
                    continue
                if verbosity or not ignore_errors:
                    M = M or len(row)
                    if len(row) != M:
                        print 'ERROR importing row #%d in batch_num=%d which is row #%d overall. The row had %d columns, but previous rows had %d.' % (j + 1, batch_num + 1, i + j + 1, len(row), M)
                        print 'Erroneously parsed row:'
                        print repr(row)
                        
                        if not ignore_errors:
                            raise ValueError('ERROR importing row #%d which had %d columns, but previous rows had %d.' % (i + j + 1, len(row), M))
                try:
                    obj, row_errors = django_object_from_row(row, model=model, field_names=field_names, strip=strip, verbosity=verbosity)
                    batch_of_objects += [obj]
                    errors += row_errors
                except:
                    if verbosity > 0:
                        print 'Error importing row #%d' % (i + j + 1)
                        print_exc()
                    if not ignore_errors:
                        raise
                if verbosity > 0:
                    try:
                        pbar.update(i + j)
                    except:
                        print_exc()
                        if not ignore_errors:
                            raise
            i += len(batch_of_rows)
            if not dry_run:
                model.objects.bulk_create(batch_of_objects)
            elif verbosity > 0:
                print "DRY_RUN: NOT bulk creating batch of %d records in %r" % (len(batch_of_objects), model)
        if verbosity > 0:
            pbar.finish()
    return i
668476
def interface_direct_csvpath(csvpath):
    """"""
    with open(csvpath) as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            data_class = row.pop('amaasclass', '')
            return interface_direct_class(data_class)
1158682
def reorder(args):
    
    import csv

    p = OptionParser(reorder.__doc__)
    p.set_sep()
    opts, args = p.parse_args(args)

    if len(args) != 2:
        sys.exit(not p.print_help())

    tabfile, order = args
    sep = opts.sep
    order = [int(x) - 1 for x in order.split(",")]
    reader = csv.reader(must_open(tabfile), delimiter=sep)
    writer = csv.writer(sys.stdout, delimiter=sep)
    for row in reader:
        newrow = [row[x] for x in order]
        writer.writerow(newrow)
1355248
def from_csv(cls, path:PathOrStr, csv_name, valid_pct:float=0.2, test:Optional[str]=None,
                 tokenizer:Tokenizer=None, vocab:Vocab=None, classes:Collection[str]=None, delimiter:str=None, header='infer',
                 text_cols:IntsOrStrs=1, label_cols:IntsOrStrs=0, label_delim:str=None,
                 chunksize:int=10000, max_vocab:int=60000, min_freq:int=2, 
                 mark_fields:bool=False, include_bos:bool=True, include_eos:bool=False, **kwargs) -> DataBunch:
        ""
        df = pd.read_csv(Path(path)/csv_name, header=header, delimiter=delimiter)
        df = df.iloc[np.random.permutation(len(df))]
        cut = int(valid_pct * len(df)) + 1
        train_df, valid_df = df[cut:], df[:cut]
        test_df = None if test is None else pd.read_csv(Path(path)/test, header=header, delimiter=delimiter)
        return cls.from_df(path, train_df, valid_df, test_df, tokenizer=tokenizer, vocab=vocab, classes=classes, text_cols=text_cols,
                           label_cols=label_cols, label_delim=label_delim, chunksize=chunksize, max_vocab=max_vocab,
                           min_freq=min_freq, mark_fields=mark_fields, 
                           include_bos=include_bos, include_eos=include_eos, **kwargs)
272395
def csv_tolist(path_to_file, **kwargs):
    

    result = []

    encoding = kwargs.get('encoding', 'utf-8')
    delimiter = kwargs.get('delimiter', ',')
    dialect = kwargs.get('dialect', csv.excel)

    _, _ext = path_to_file.split('.', 1)

    try:

        file = codecs.open(path_to_file, 'r', encoding)
        items_file = io.TextIOWrapper(file, encoding=encoding)
        result = list(
            csv.reader(items_file, delimiter=delimiter, dialect=dialect))

        items_file.close()
        file.close()

    except Exception as ex:
        result = []
        logger.error('Fail parsing csv to list of rows - {}'.format(ex))

    return result
1339983
def rec2csv(r, filename):
    """"""
    names = r.dtype.names
    def translate(x):
        if x is None or str(x).lower == "none":
            x = ""
        return str(x)
    with open(filename, "w") as csv:
        csv.write(",".join([str(x) for x in names])+"\n")
        for data in r:
            csv.write(",".join([translate(x) for x in data])+"\n")
    #print "Wrote CSV table %r" % filename
    return filename
587262
def fh_to_record(self, f):
        """"""
        import unicodecsv as csv

        fn_path = self.file_name

        fr = self.record
        fr.path = fn_path
        rows = []

        # NOTE. There were two cases here, for PY2 and PY3. Py two had
        # encoding='utf-8' in the reader. I've combined them b/c that's the default for
        # unicode csv, so it shouldn't be necessary.

        # Should probably be something like this:
        #if sys.version_info[0] >= 3:  # Python 3
        #    import csv
        #    f = open(self._fstor.syspath, 'rtU', encoding=encoding)
        #    reader = csv.reader(f)
        #else:  # Python 2
        #    import unicodecsv as csv
        #    f = open(self._fstor.syspath, 'rbU')
        #    reader = csv.reader(f, encoding=encoding)

        for row in csv.reader(f):
            row = [e if e.strip() != '' else None for e in row]
            if any(bool(e) for e in row):
                rows.append(row)
        try:
            fr.update_contents(msgpack.packb(rows), 'application/msgpack')
        except AssertionError:
            raise

        fr.source_hash = self.fs_hash
        fr.synced_fs = self.fs_modtime
        fr.modified = self.fs_modtime
691941
def cli(*args, **kwargs):
    
    # Convert CSV file
    content = convert.convert(kwargs["input_file"], **kwargs)

    # Serve the temporary file in browser.
    if kwargs["serve"]:
        convert.serve(content)
    # Write to output file
    elif kwargs["output_file"]:
        # Check if file can be overwrite
        if (not kwargs["overwrite"] and
                not prompt_overwrite(kwargs["output_file"])):
            raise click.Abort()

        convert.save(kwargs["output_file"], content)
        click.secho("File converted successfully: {}".format(
            kwargs["output_file"]), fg="green")
    else:
        # If its not server and output file is missing then raise error
        raise click.BadOptionUsage("Missing argument \"output_file\".")
642830
def MPTfileCSV(file_or_path):
    

    if isinstance(file_or_path, str):
        mpt_file = open(file_or_path, 'r')
    else:
        mpt_file = file_or_path

    magic = next(mpt_file)
    if magic.rstrip() != 'EC-Lab ASCII FILE':
        raise ValueError("Bad first line for EC-Lab file: '%s'" % magic)

    nb_headers_match = re.match('Nb header lines : (\d+)\s*$', next(mpt_file))
    nb_headers = int(nb_headers_match.group(1))
    if nb_headers < 3:
        raise ValueError("Too few header lines: %d" % nb_headers)

    ## The 'magic number' line, the 'Nb headers' line and the column headers
    ## make three lines. Every additional line is a comment line.
    comments = [next(mpt_file) for i in range(nb_headers - 3)]

    mpt_csv = csv.DictReader(mpt_file, dialect='excel-tab')

    expected_fieldnames = (
        ["mode", "ox/red", "error", "control changes", "Ns changes",
         "counter inc.", "time/s", "control/V/mA", "Ewe/V", "dq/mA.h",
         "P/W", "<I>/mA", "(Q-Qo)/mA.h", "x"],
        ['mode', 'ox/red', 'error', 'control changes', 'Ns changes',
         'counter inc.', 'time/s', 'control/V', 'Ewe/V', 'dq/mA.h',
         '<I>/mA', '(Q-Qo)/mA.h', 'x'],
        ["mode", "ox/red", "error", "control changes", "Ns changes",
         "counter inc.", "time/s", "control/V", "Ewe/V", "I/mA",
         "dQ/mA.h", "P/W"],
        ["mode", "ox/red", "error", "control changes", "Ns changes",
         "counter inc.", "time/s", "control/V", "Ewe/V", "<I>/mA",
         "dQ/mA.h", "P/W"])
    if mpt_csv.fieldnames not in expected_fieldnames:
        raise ValueError("Unrecognised headers for MPT file format")

    return mpt_csv, comments
261020
function processCsv (csvContent) {
  UNSAFE_SYMBOLS.forEach(function (symbolParams) {
    csvContent = csvContent.replace(symbolParams.pattern, symbolParams.replace);
  });
  return csvContent;
}
1043700
def _dialect(self, filepath):
        
        with open(filepath, self.read_mode) as csvfile:
            sample = csvfile.read(1024)
            dialect = csv.Sniffer().sniff(sample)
            if self.has_header == None:
                # detect header if header not specified
                self.has_header = csv.Sniffer().has_header(sample)
            csvfile.seek(0)
        return dialect
378160
def parse_options(argv:)
      input_path = nil
      columns = nil
      output_path = nil
      delimiter = ','
      header_only = false
      rows_only = false
      skip_lines = nil
      type_map = {}

      OptionParser.new do |parser|
        @parser = parser

        parser.banner = 'Usage: honey_format [options] <file.csv>'
        parser.default_argv = argv

        parser.on('--csv=input.csv', String, 'CSV file') do |value|
          input_path = value
        end

        parser.on('--columns=id,name', Array, 'Select columns') do |value|
          columns = value&.map(&:to_sym)
        end

        parser.on('--output=output.csv', String, 'CSV output (STDOUT otherwise)') do |value|
          output_path = value
        end

        parser.on('--delimiter=,', String, 'CSV delimiter (default: ,)') do |value|
          delimiter = value
        end

        parser.on('--skip-lines=,', String, 'Skip lines that match this pattern') do |value|
          skip_lines = value
        end

        parser.on('--type-map=[key1=val1,key2=val2]', Array, 'Type map') do |value|
          type_map = option_to_h(value || [])
        end

        parser.on('--[no-]header-only', 'Print only the header') do |value|
          header_only = value
        end

        parser.on('--[no-]rows-only', 'Print only the rows') do |value|
          rows_only = value
        end

        parser.on('-h', '--help', 'How to use') do
          puts parser
          exit
        end

        parser.on_tail('--version', 'Show version') do
          puts "HoneyFormat version #{HoneyFormat::VERSION}"
          exit
        end
      end.parse!

      if header_only && rows_only
        raise(ArgumentError, "you can't provide both --header-only and --rows-only")
      end

      if input_path && argv.last
        raise(ArgumentError, "you can't provide both --csv and <path>")
      end

      input_path ||= argv.last

      {
        input_path: input_path,
        columns: columns,
        output_path: output_path,
        delimiter: delimiter,
        header_only: header_only,
        rows_only: rows_only,
        skip_lines: skip_lines,
        type_map: type_map,
      }
    end
719591
def dialectfromstring(s):
    
    try:
        AST = compiler.parse(s)
    except SyntaxError:
        return
    else:
        try:
            if (len(AST.getChildren()) > 1):
                ST = AST.getChildren()[1]
                if isinstance(ST, Stmt):
                    if isinstance(ST.getChildren()[0], Discard):
                        d = ST.getChildren()[0].asList()[0]
        except (TypeError,AttributeError):
            pass
        else:
            if (isinstance(d,Dict) and (len(d.items) > 0)):
                if all([isctype(i[0], str) for i in d.items]):
                    testd = csv.Sniffer().sniff('a,b,c')
                    if all([n.value in dir(testd) and 
                        isctype(v, type(getattr(testd, n.value))) for (n,v) in 
                                                                      d.items]):
                        D = eval(s)
                        for n in D.keys():
                            setattr(testd, n, D[n])
                        return testd
91997
def fill(self, data, dialect):
        

        csvfile = cStringIO.StringIO()
        csvwriter = csv.writer(csvfile, dialect=dialect)

        for i, line in enumerate(data):
            csvwriter.writerow(list(encode_gen(line)))
            if i >= self.preview_lines:
                break

        preview = csvfile.getvalue()
        csvfile.close()
        preview = preview.decode("utf-8").replace("\r\n", "\n")
        self.SetValue(preview)