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


Python Group.attrs方法代碼示例

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


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

示例1: read_ascii

# 需要導入模塊: from larch import Group [as 別名]
# 或者: from larch.Group import attrs [as 別名]
def read_ascii(fname, labels=None, sort=False, ilabels=False, sort_column=0, _larch=None):
    """read a column ascii column file, returning a group containing the data from the file.

    read_ascii(filename, labels=None, sort=False, ilabels=False, sort_column=0)

    If the header is one of the forms of
        KEY : VAL
        KEY = VAL
    these will be parsed into a 'attrs' dictionary in the returned group.

    If labels is left the default value of None, column labels will be tried to
    be created from the line immediately preceeding the data, or using 'col1', 'col2',
    etc if column labels cannot be figured out.   The labels will be used to create
    1-d arrays for each column.  If ilabels is True, the names 'col1', 'col2' etc will
    be used regardless of the column labels found in the file.

    The group will have a 'data' component containing the 2-dimensional data, it will also
    have a 'header' component containing the text of the header -- an array of lines.
    If a footer (text after the block of numerical data) is in the file, the array of
    lines for this text will be put in the 'footer' component.

    """
    if not os.path.isfile(fname):
        raise OSError("File not found: '%s'" % fname)
    if os.stat(fname).st_size > MAX_FILESIZE:
        raise OSError("File '%s' too big for read_ascii()" % fname)

    finp = open(fname, 'r')
    text = finp.readlines()
    finp.close()

    _labelline, ncol = None, None
    data, footers, headers = [], [], []

    text.reverse()
    section = 'FOOTER'

    for line in text:
        line = line[:-1].strip()
        if len(line) < 1:
            continue
        # look for section transitions (going from bottom to top)
        if section == 'FOOTER' and getfloats(line) is not None:
            section = 'DATA'
        elif section == 'DATA' and getfloats(line) is None:
            section = 'HEADER'
            _labelline = line
        # act of current section:
        if section == 'FOOTER':
            footers.append(line)
        elif section == 'HEADER':
            headers.append(line)
        elif section == 'DATA':
            rowdat  = getfloats(line)
            if ncol is None:
                ncol = len(rowdat)
            if ncol == len(rowdat):
                data.append(rowdat)
    if ilabels:
        _labelline = None

    # reverse header, footer, data, convert to arrays
    footers.reverse()
    headers.reverse()
    data.reverse()
    data = np.array(data).transpose()

    # try to parse attributes from header text
    header_attrs = {}
    for hline in headers:
        hline = hline.strip().replace('\t', ' ')
        if len(hline) < 1: continue
        if hline[0] in COMMENTCHARS:
            hline = hline[1:].strip()
        keywds = []
        if ':' in hline: # keywords in  'x: 22'
            words = hline.split(':', 1)
            keywds = words[0].split()
        elif '=' in hline: # keywords in  'x = 22'
            words = hline.split('=', 1)
            keywds = words[0].split()
        if len(keywds) == 1:
            key = colname(keywds[0])
            if key.startswith('_'):
                key = key[1:]
            if len(words) > 1:
                header_attrs[key] = words[1].strip()

    ncols, nrow = data.shape
    # set column labels
    _labels = ['col%i' % (i+1) for i in range(ncols)]
    if labels is None:
        if _labelline is None:
            _labelline = ' '.join(_labels)
        if _labelline[0] in COMMENTCHARS:
            _labelline = _labelline[1:].strip()
        _labelline = _labelline.lower()
        try:
            labels = [colname(l) for l in _labelline.split()]
        except:
#.........這裏部分代碼省略.........
開發者ID:bruceravel,項目名稱:xraylarch,代碼行數:103,代碼來源:columnfile.py

示例2: read_ascii

# 需要導入模塊: from larch import Group [as 別名]
# 或者: from larch.Group import attrs [as 別名]
def read_ascii(fname, labels=None, simple_labels=False, sort=False, sort_column=0, _larch=None):
    """read a column ascii column file, returning a group containing the data from the file.

    read_ascii(filename, labels=None, simple_labels=False, sort=False, sort_column=0)

    Arguments
    ---------
     fname (str)           name of file to read
     labels (list or None) list of labels to use for column labels [None]
     simple_labels (bool)  whether to force simple column labels (note 1) [False]
     sort (bool)           whether to sort row data (note 2) [False]
     sort_column (int)     column to use for sorting (note 2) [0]

    Returns
    --------
      group containing data read from file

    Notes
    -----
      1. column labels.  If `labels` is left the default value of `None`,
         column labels will be tried to be created from the line
         immediately preceeding the data, or using 'col1', 'col2', etc if
         column labels cannot be figured out.  The labels will be used as
         names for the 1-d arrays for each column.  If `simple_labels` is
         `True`, the names 'col1', 'col2' etc will be used regardless of
         the column labels found in the file.

      2. sorting.  Data can be sorted to be in increasing order of any column,
         by giving the column index (starting from 0).

      3. header parsing. If header lineas are of the forms of
            KEY : VAL
            KEY = VAL
         these will be parsed into a 'attrs' dictionary in the returned group.


    The returned group will have a number of members:

       GROUP.filename: text name of the file
       GROUP.column_labels: column labels, names of 1-D arrays
       GROUP.data:     2-dimensional data (ncolumns, nrows)
       GROUP.header:   array of text lines of the header.
       GROUP.footer:   array of text lines of the footer (text after the block of numerical data)
       GROUP.attrs :   group of attributes parsed from header lines
    """
    if not os.path.isfile(fname):
        raise OSError("File not found: '%s'" % fname)
    if os.stat(fname).st_size > MAX_FILESIZE:
        raise OSError("File '%s' too big for read_ascii()" % fname)

    finp = open(fname, "r")
    text = finp.readlines()
    finp.close()

    _labelline, ncol = None, None
    data, footers, headers = [], [], []

    text.reverse()
    section = "FOOTER"

    for line in text:
        line = line[:-1].strip()
        if len(line) < 1:
            continue
        # look for section transitions (going from bottom to top)
        if section == "FOOTER" and getfloats(line) is not None:
            section = "DATA"
        elif section == "DATA" and getfloats(line) is None:
            section = "HEADER"
            _labelline = line
        # act of current section:
        if section == "FOOTER":
            footers.append(line)
        elif section == "HEADER":
            headers.append(line)
        elif section == "DATA":
            rowdat = getfloats(line)
            if ncol is None:
                ncol = len(rowdat)
            if ncol == len(rowdat):
                data.append(rowdat)
    if simple_labels:
        _labelline = None

    # reverse header, footer, data, convert to arrays
    footers.reverse()
    headers.reverse()
    data.reverse()
    data = np.array(data).transpose()

    # try to parse attributes from header text
    header_attrs = {}
    for hline in headers:
        hline = hline.strip().replace("\t", " ")
        if len(hline) < 1:
            continue
        if hline[0] in COMMENTCHARS:
            hline = hline[1:].strip()
        keywds = []
        if ":" in hline:  # keywords in  'x: 22'
#.........這裏部分代碼省略.........
開發者ID:,項目名稱:,代碼行數:103,代碼來源:

示例3: read_ascii

# 需要導入模塊: from larch import Group [as 別名]
# 或者: from larch.Group import attrs [as 別名]
def read_ascii(filename, labels=None, simple_labels=False,
               sort=False, sort_column=0, delimeter=None, _larch=None):
    """read a column ascii column file, returning a group containing the data
    extracted from the file.

    read_ascii(filename, labels=None, simple_labels=False, sort=False, sort_column=0)

    Arguments
    ---------
     filename (str)        name of file to read
     labels (list or None) list of labels to use for column labels [None]
     simple_labels (bool)  whether to force simple column labels (note 1) [False]
     delimeter (str)       string to use to split label line
     sort (bool)           whether to sort row data (note 2) [False]
     sort_column (int)     column to use for sorting (note 2) [0]

    Returns
    --------
      group containing data read from file

    Notes
    -----
      1. column labels.  If `labels` is left the default value of `None`,
         column labels will be tried to be created from the line
         immediately preceeding the data and the provided delimeter, and may
         use 'col1', 'col2', etc if suitable column labels cannot be figured out.
         The labels will be used as names for the 1-d arrays for each column.
         If `simple_labels` is  `True`, the names 'col1', 'col2' etc will be used
         regardless of the column labels found in the file.

      2. sorting.  Data can be sorted to be in increasing order of any column,
         by giving the column index (starting from 0).

      3. header parsing. If header lines are of the forms of
            KEY : VAL
            KEY = VAL
         these will be parsed into a 'attrs' dictionary in the returned group.


    The returned group will have a number of members:

       GROUP.filename: text name of the file
       GROUP.array_labels: array labels, names of 1-D arrays
       GROUP.data:     2-dimensional data (ncolumns, nrows)
       GROUP.header:   array of text lines of the header.
       GROUP.footer:   array of text lines of the footer (text after the block of numerical data)
       GROUP.attrs :   group of attributes parsed from header lines
    """
    if not os.path.isfile(filename):
        raise OSError("File not found: '%s'" % filename)
    if os.stat(filename).st_size > MAX_FILESIZE:
        raise OSError("File '%s' too big for read_ascii()" % filename)

    with open(filename, 'r') as fh:
        text = fh.read()

    text = text.replace('\r\n', '\n').replace('\r', '\n').split('\n')

    labelline = None
    ncol = None
    data, footers, headers = [], [], []

    text.reverse()
    section = 'FOOTER'

    for line in text:
        line = line.strip()
        if len(line) < 1:
            continue
        # look for section transitions (going from bottom to top)
        if section == 'FOOTER' and not None in getfloats(line):
            section = 'DATA'
        elif section == 'DATA' and None in getfloats(line):
            section = 'HEADER'
            labelline = line
            if labelline[0] in COMMENTCHARS:
                labelline = labelline[1:].strip()
        # act of current section:
        if section == 'FOOTER':
            footers.append(line)
        elif section == 'HEADER':
            headers.append(line)
        elif section == 'DATA':
            rowdat  = getfloats(line)
            if ncol is None:
                ncol = len(rowdat)
            elif ncol > len(rowdat):
                rowdat.extend([np.nan]*(ncol-len(rowdat)))
            elif ncol < len(rowdat):
                for i in data:
                    i.extend([np.nan]*(len(rowdat)-ncol))
                ncol = len(rowdat)
            data.append(rowdat)

    # reverse header, footer, data, convert to arrays
    footers.reverse()
    headers.reverse()
    data.reverse()
    data = np.array(data).transpose()

#.........這裏部分代碼省略.........
開發者ID:maurov,項目名稱:xraylarch,代碼行數:103,代碼來源:columnfile.py


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