当前位置: 首页>>代码示例>>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;未经允许,请勿转载。