MCPcopy Create free account
hub / github.com/numpy/numpy / fromtextfile

Function fromtextfile

numpy/ma/mrecords.py:669–746  ·  view source on GitHub ↗

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,
                 *, delimitor=np._NoValue)

Source from the content-addressed store, hash-verified

667
668
669def fromtextfile(fname, delimiter=None, commentchar='#', missingchar='',
670 varnames=None, vartypes=None,
671 *, delimitor=np._NoValue): # backwards compatibility
672 """
673 Creates a mrecarray from data stored in the file `filename`.
674
675 Parameters
676 ----------
677 fname : {file name/handle}
678 Handle of an opened file.
679 delimiter : {None, string}, optional
680 Alphanumeric character used to separate columns in the file.
681 If None, any (group of) white spacestring(s) will be used.
682 commentchar : {'#', string}, optional
683 Alphanumeric character used to mark the start of a comment.
684 missingchar : {'', string}, optional
685 String indicating missing data, and used to create the masks.
686 varnames : {None, sequence}, optional
687 Sequence of the variable names. If None, a list will be created from
688 the first non empty line of the file.
689 vartypes : {None, sequence}, optional
690 Sequence of the variables dtypes. If None, it will be estimated from
691 the first non-commented line.
692
693
694 Ultra simple: the varnames are in the header, one line"""
695 if delimitor is not np._NoValue:
696 if delimiter is not None:
697 raise TypeError("fromtextfile() got multiple values for argument "
698 "'delimiter'")
699 # NumPy 1.22.0, 2021-09-23
700 warnings.warn("The 'delimitor' keyword argument of "
701 "numpy.ma.mrecords.fromtextfile() is deprecated "
702 "since NumPy 1.22.0, use 'delimiter' instead.",
703 DeprecationWarning, stacklevel=2)
704 delimiter = delimitor
705
706 # Try to open the file.
707 ftext = openfile(fname)
708
709 # Get the first non-empty line as the varnames
710 while True:
711 line = ftext.readline()
712 firstline = line[:line.find(commentchar)].strip()
713 _varnames = firstline.split(delimiter)
714 if len(_varnames) > 1:
715 break
716 if varnames is None:
717 varnames = _varnames
718
719 # Get the data.
720 _variables = masked_array([line.strip().split(delimiter) for line in ftext
721 if line[0] != commentchar and len(line) > 1])
722 (_, nfields) = _variables.shape
723 ftext.close()
724
725 # Try to guess the dtype.
726 if vartypes is None:

Callers 1

test_fromtextfileMethod · 0.90

Calls 9

openfileFunction · 0.85
_guessvartypesFunction · 0.85
warnMethod · 0.80
stripMethod · 0.80
findMethod · 0.80
fromarraysFunction · 0.70
splitMethod · 0.45
closeMethod · 0.45
dtypeMethod · 0.45

Tested by 1

test_fromtextfileMethod · 0.72