Creates a mrecarray from data stored in the file `filename`. Parameters ---------- fname : {file name/handle} Handle of an opened file. delimiter : {None, string}, optional Alphanumeric character used to separate columns in the file. If None, any (group
(fname, delimiter=None, commentchar='#', missingchar='',
varnames=None, vartypes=None)
| 634 | |
| 635 | |
| 636 | def fromtextfile(fname, delimiter=None, commentchar='#', missingchar='', |
| 637 | varnames=None, vartypes=None): |
| 638 | """ |
| 639 | Creates a mrecarray from data stored in the file `filename`. |
| 640 | |
| 641 | Parameters |
| 642 | ---------- |
| 643 | fname : {file name/handle} |
| 644 | Handle of an opened file. |
| 645 | delimiter : {None, string}, optional |
| 646 | Alphanumeric character used to separate columns in the file. |
| 647 | If None, any (group of) white spacestring(s) will be used. |
| 648 | commentchar : {'#', string}, optional |
| 649 | Alphanumeric character used to mark the start of a comment. |
| 650 | missingchar : {'', string}, optional |
| 651 | String indicating missing data, and used to create the masks. |
| 652 | varnames : {None, sequence}, optional |
| 653 | Sequence of the variable names. If None, a list will be created from |
| 654 | the first non empty line of the file. |
| 655 | vartypes : {None, sequence}, optional |
| 656 | Sequence of the variables dtypes. If None, it will be estimated from |
| 657 | the first non-commented line. |
| 658 | |
| 659 | |
| 660 | Ultra simple: the varnames are in the header, one line""" |
| 661 | |
| 662 | # Try to open the file. |
| 663 | ftext = openfile(fname) |
| 664 | |
| 665 | # Get the first non-empty line as the varnames |
| 666 | while True: |
| 667 | line = ftext.readline() |
| 668 | firstline = line[:line.find(commentchar)].strip() |
| 669 | _varnames = firstline.split(delimiter) |
| 670 | if len(_varnames) > 1: |
| 671 | break |
| 672 | if varnames is None: |
| 673 | varnames = _varnames |
| 674 | |
| 675 | # Get the data. |
| 676 | _variables = ma.masked_array([line.strip().split(delimiter) for line in ftext |
| 677 | if line[0] != commentchar and len(line) > 1]) |
| 678 | (_, nfields) = _variables.shape |
| 679 | ftext.close() |
| 680 | |
| 681 | # Try to guess the dtype. |
| 682 | if vartypes is None: |
| 683 | vartypes = _guessvartypes(_variables[0]) |
| 684 | else: |
| 685 | vartypes = [np.dtype(v) for v in vartypes] |
| 686 | if len(vartypes) != nfields: |
| 687 | msg = f"Attempting to {len(vartypes)} dtypes for {nfields} fields!" |
| 688 | msg += " Reverting to default." |
| 689 | warnings.warn(msg, stacklevel=2) |
| 690 | vartypes = _guessvartypes(_variables[0]) |
| 691 | |
| 692 | # Construct the descriptor. |
| 693 | mdescr = list(zip(varnames, vartypes)) |
searching dependent graphs…