當前位置: 首頁>>代碼示例>>Python>>正文


Python csv.Sniffer方法代碼示例

本文整理匯總了Python中csv.Sniffer方法的典型用法代碼示例。如果您正苦於以下問題:Python csv.Sniffer方法的具體用法?Python csv.Sniffer怎麽用?Python csv.Sniffer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在csv的用法示例。


在下文中一共展示了csv.Sniffer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _modify_column_headers

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def _modify_column_headers(tmp_file_location,
                               custom_dimension_header_mapping):
        logger.info('Modifying column headers to be compatible for data upload')
        with open(tmp_file_location, 'r') as check_header_file:
            has_header = csv.Sniffer().has_header(check_header_file.read(1024))
            if has_header:
                with open(tmp_file_location, 'r') as read_file:
                    reader = csv.reader(read_file)
                    headers = next(reader)
                    new_headers = []
                    for header in headers:
                        if header in custom_dimension_header_mapping:
                            header = custom_dimension_header_mapping.get(header)
                        new_header = 'ga:' + header
                        new_headers.append(new_header)
                    all_data = read_file.readlines()
                    final_headers = ','.join(new_headers) + '\n'
                    all_data.insert(0, final_headers)
                    with open(tmp_file_location, 'w') as write_file:
                        write_file.writelines(all_data)
            else:
                raise NameError('CSV does not contain headers, please add them '
                                'to use the modify column headers functionality') 
開發者ID:google,項目名稱:orchestra,代碼行數:25,代碼來源:google_analytics.py

示例2: csv_to_dmatrix

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def csv_to_dmatrix(string_like, dtype=None):  # type: (str) -> xgb.DMatrix
    """Convert a CSV object to a DMatrix object.
    Args:
        string_like (str): CSV string. Assumes the string has been stripped of leading or trailing newline chars.
        dtype (dtype, optional):  Data type of the resulting array. If None, the dtypes will be determined by the
                                        contents of each column, individually. This argument can only be used to
                                        'upcast' the array.  For downcasting, use the .astype(t) method.
    Returns:
        (xgb.DMatrix): XGBoost DataMatrix
    """
    sniff_delimiter = csv.Sniffer().sniff(string_like.split('\n')[0][:512]).delimiter
    delimiter = ',' if sniff_delimiter.isalnum() else sniff_delimiter
    logging.info("Determined delimiter of CSV input is \'{}\'".format(delimiter))

    np_payload = np.array(list(map(lambda x: _clean_csv_string(x, delimiter), string_like.split('\n')))).astype(dtype)
    return xgb.DMatrix(np_payload) 
開發者ID:aws,項目名稱:sagemaker-xgboost-container,代碼行數:18,代碼來源:encoder.py

示例3: from_csv

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def from_csv(fp, field_names = None, **kwargs):

    dialect = csv.Sniffer().sniff(fp.read(1024))
    fp.seek(0)
    reader = csv.reader(fp, dialect)

    table = PrettyTable(**kwargs)
    if field_names:
        table.field_names = field_names
    else:
        if py3k:
            table.field_names = [x.strip() for x in next(reader)]
        else:
            table.field_names = [x.strip() for x in reader.next()]

    for row in reader:
        table.add_row([x.strip() for x in row])

    return table 
開發者ID:vulscanteam,項目名稱:vulscan,代碼行數:21,代碼來源:prettytable.py

示例4: test_delimiters

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def test_delimiters(self):
        sniffer = csv.Sniffer()
        dialect = sniffer.sniff(self.sample3)
        # given that all three lines in sample3 are equal,
        # I think that any character could have been 'guessed' as the
        # delimiter, depending on dictionary order
        self.assertIn(dialect.delimiter, self.sample3)
        dialect = sniffer.sniff(self.sample3, delimiters="?,")
        self.assertEqual(dialect.delimiter, "?")
        dialect = sniffer.sniff(self.sample3, delimiters="/,")
        self.assertEqual(dialect.delimiter, "/")
        dialect = sniffer.sniff(self.sample4)
        self.assertEqual(dialect.delimiter, ";")
        dialect = sniffer.sniff(self.sample5)
        self.assertEqual(dialect.delimiter, "\t")
        dialect = sniffer.sniff(self.sample6)
        self.assertEqual(dialect.delimiter, "|")
        dialect = sniffer.sniff(self.sample7)
        self.assertEqual(dialect.delimiter, "|")
        self.assertEqual(dialect.quotechar, "'")
        dialect = sniffer.sniff(self.sample8)
        self.assertEqual(dialect.delimiter, '+')
        dialect = sniffer.sniff(self.sample9)
        self.assertEqual(dialect.delimiter, '+')
        self.assertEqual(dialect.quotechar, "'") 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:27,代碼來源:test_csv.py

示例5: __init__

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def __init__(self, fname, labels):
        """
        Initialize the corpus from a file.
        `labels` = are class labels present in the input file? => skip the first column

        """
        logger.info("loading corpus from %s" % fname)
        self.fname = fname
        self.length = None
        self.labels = labels

        # load the first few lines, to guess the CSV dialect
        head = ''.join(itertools.islice(open(self.fname), 5))
        self.headers = csv.Sniffer().has_header(head)
        self.dialect = csv.Sniffer().sniff(head)
        logger.info("sniffed CSV delimiter=%r, headers=%s" % (self.dialect.delimiter, self.headers)) 
開發者ID:largelymfs,項目名稱:topical_word_embeddings,代碼行數:18,代碼來源:csvcorpus.py

示例6: test_delimiters

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def test_delimiters(self):
        sniffer = csv.Sniffer()
        dialect = sniffer.sniff(self.sample3)
        # given that all three lines in sample3 are equal,
        # I think that any character could have been 'guessed' as the
        # delimiter, depending on dictionary order
        self.assertIn(dialect.delimiter, self.sample3)
        dialect = sniffer.sniff(self.sample3, delimiters="?,")
        self.assertEqual(dialect.delimiter, "?")
        dialect = sniffer.sniff(self.sample3, delimiters="/,")
        self.assertEqual(dialect.delimiter, "/")
        dialect = sniffer.sniff(self.sample4)
        self.assertEqual(dialect.delimiter, ";")
        dialect = sniffer.sniff(self.sample5)
        self.assertEqual(dialect.delimiter, "\t")
        dialect = sniffer.sniff(self.sample6)
        self.assertEqual(dialect.delimiter, "|")
        dialect = sniffer.sniff(self.sample7)
        self.assertEqual(dialect.delimiter, "|")
        self.assertEqual(dialect.quotechar, "'") 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:22,代碼來源:test_csv.py

示例7: __init__

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def __init__(
            self,
            csv_file_path,
            converter=None,
            callbacks={}
    ):
        super().__init__(converter=converter, callbacks=callbacks)

        self.csv_file_path = csv_file_path
        self.csvfile = open(self.csv_file_path)
        self.sniffer = csv.Sniffer()
        self.dialect = self.sniffer.sniff(self.csvfile.read(1024))
        self.csvfile.seek(0)
        self.has_header = self.sniffer.has_header(self.csvfile.read(1024))
        self.csvfile.seek(0)
        self.reader = csv.DictReader(self.csvfile, dialect=self.dialect)
        if self.has_header:
            print('=======')
            print(self.reader.fieldnames)
            print('=======')
        else:
            # should raise error?
            pass 
開發者ID:Altinity,項目名稱:clickhouse-mysql-data-reader,代碼行數:25,代碼來源:csvreader.py

示例8: __init__

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def __init__(self, path, sep=' ', **kwargs):
        self._node_ids = None
        self._min_time = None
        self._max_time = None
        # self._dt = None

        try:
            # check to see if file contains headers
            with open(path, 'r') as csvfile:
                sniffer = csv.Sniffer()
                has_headers = sniffer.has_header(csvfile.read(1024))
        except Exception:
            has_headers = True

        self._spikes_df = pd.read_csv(path, sep=sep, header=0 if has_headers else None)

        if not has_headers:
            self._spikes_df.columns = csv_headers[:self._spikes_df.shape[1]]

        if col_population not in self._spikes_df.columns:
            pop_name = kwargs.get(col_population, pop_na)
            self._spikes_df[col_population] = pop_name 
開發者ID:AllenInstitute,項目名稱:sonata,代碼行數:24,代碼來源:spike_trains_file.py

示例9: __init__

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def __init__(self, path, sep=' ', **kwargs):
        self._n_spikes = None
        self._populations = None

        try:
            # check to see if file contains headers
            with open(path, 'r') as csvfile:
                sniffer = csv.Sniffer()
                has_headers = sniffer.has_header(csvfile.read(1024))
        except Exception:
            has_headers = True

        self._spikes_df = pd.read_csv(path, sep=sep, header=0 if has_headers else None)

        if not has_headers:
            self._spikes_df.columns = csv_headers[0::2]

        if col_population not in self._spikes_df.columns:
            pop_name = kwargs.get(col_population, pop_na)
            self._spikes_df[col_population] = pop_name

        # TODO: Check all the necessary columns exits
        self._spikes_df = self._spikes_df[csv_headers] 
開發者ID:AllenInstitute,項目名稱:sonata,代碼行數:25,代碼來源:csv_adaptors.py

示例10: get_dialect

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def get_dialect(file_obj, sample_size=1024*1024):
    try:
        sample = file_obj.read(sample_size)
    except StopIteration:
        sample = file_obj.readline()
    file_obj.seek(0)

    sniffer = csv.Sniffer()
    dialect = sniffer.sniff(sample)

    result = {
        'delimiter': dialect.delimiter,
        'doublequote': dialect.doublequote,
        'escapechar': dialect.escapechar,
        'lineterminator': dialect.lineterminator,
        'quotechar': dialect.quotechar,
    }
    return result 
開發者ID:MacroConnections,項目名稱:DIVE-backend,代碼行數:20,代碼來源:upload.py

示例11: csvfile_to_wb

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def csvfile_to_wb(csv_filename):
    '''Open a CSV file and return an openpyxl workbook.'''

    logger.log(
        DEBUG_DETAILED,
        'Converting CSV file {} into an XLSX workbook.'.format(csv_filename))

    with open(csv_filename) as csv_file:
        dialect = csv.Sniffer().sniff(csv_file.read())
        if USING_PYTHON2:
            for attr in dir(dialect):
                a = getattr(dialect, attr)
                if type(a) == unicode:
                    setattr(dialect, attr, bytes(a))
        csv_file.seek(0)
        reader = csv.reader(csv_file, dialect)
        wb = pyxl.Workbook()
        ws = wb.active
        for row_index, row in enumerate(reader, 1):
            for column_index, cell in enumerate(row, 1):
                if cell not in ('', None):
                    ws.cell(row=row_index, column=column_index).value = cell
    return (wb, dialect) 
開發者ID:xesscorp,項目名稱:KiField,代碼行數:25,代碼來源:kifield.py

示例12: test_delimited

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def test_delimited(filepath):
    import csv
    if six.PY3:
        handle = open(filepath, 'r', newline='')
    else:
        handle = open(filepath, 'rb')
    with handle as csv_file:
        try:
            dialect = csv.Sniffer().sniff(csv_file.read(1024*16), delimiters=',\t')
        except Exception as e:
            return False, None
        csv_file.seek(0)
        reader = csv.reader(csv_file, dialect)
        rows = []
        try:
            for index, entry in enumerate(reader):
                if index == 5:
                    break
                rows.append(entry)
        except Exception as e:
            return False, None
        return True, rows 
開發者ID:Chris7,項目名稱:django-djangui,代碼行數:24,代碼來源:utils.py

示例13: get_column_value_list

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def get_column_value_list(filenames, column_name, debug=False):
        """Get the values of the given column from all filenames.

        Function that given a set of filenames returns the list of values
        concatenating all the columns with name "column_name"
        :param filenames: List of filenames
        :param column_name: Column name to search.
        :param debug: Boolean controlling the log messages.
        :return: List of values.
        """
        to_return = []
        for file_name in filenames:

            # Open the file for reading
            file_in = codecs.open(file_name, 'rU')
            dialect = csv.Sniffer().sniff(file_in.read(1024))
            file_in.seek(0)
            data_in = csv.reader(file_in, dialect=dialect, delimiter=str(','))

            if debug:
                print('Parsing file ' + file_name)

            to_return += process_csv_file(data_in, column_name)

        return to_return 
開發者ID:abelardopardo,項目名稱:ontask_b,代碼行數:27,代碼來源:initialize_db.py

示例14: sniff_for_header

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def sniff_for_header(file_path, sep="\t", comment="#"):
    """
    Warning: reads the entire file into a StringIO buffer!

    """
    with open(file_path, "r") as f:
        buf = io.StringIO(f.read())

    sample_lines = []
    for line in buf:
        if not line.startswith(comment):
            sample_lines.append(line)
            break
    for _ in range(10):
        sample_lines.append(buf.readline())
    buf.seek(0)

    has_header = csv.Sniffer().has_header("\n".join(sample_lines))
    if has_header:
        names = sample_lines[0].strip().split(sep)
    else:
        names = None

    return buf, names 
開發者ID:mirnylab,項目名稱:cooltools,代碼行數:26,代碼來源:util.py

示例15: load

# 需要導入模塊: import csv [as 別名]
# 或者: from csv import Sniffer [as 別名]
def load(self, filename: str) -> None:
        keys = None
        types = None

        with open(filename, newline='') as csvfile:
            dialect = csv.Sniffer().sniff(csvfile.read())
            csvfile.seek(0)
            reader = csv.reader(csvfile, dialect)
            for row in reader:
                if len(row) == 0:
                    continue
                if keys is None:
                    if len(row[0].split(":")) == 2:
                        keys = [r.split(':')[0] for r in row]
                        types = [r.split(':')[1] for r in row]  # NOTE: I don't use type anymore
                    else:
                        keys = row
                        types = ["None" for r in row]
                else:
                    self.items.append(Metadata(keys, row, types)) 
開發者ID:velocyto-team,項目名稱:velocyto.py,代碼行數:22,代碼來源:metadata.py


注:本文中的csv.Sniffer方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。