Separate numbers using spaces "", "$", "*" and so on, supporting strings with decimal points
Numarray=str2num (linestring,comment= ' # ')
Replace all characters in a string that are not of type double with spaces
The content that starts with ' # ' until the end of the line is emptied
Returns a one-dimensional numpy.array array
ImportNumPyImportscipydefStr2Num (linestring,comment='#'): fromIoImportStringio as StringioImportre,numpy Numarray=numpy.empty ([0],numpy.int16) Numstr=Linestring.strip ()#~ Ignore comment string forCmtinchComment:cmtre=cmt+'.*$'Numstr=re.sub (Cmtre," ", Numstr.strip (), count=0, flags=Re. IGNORECASE)#~ Delete all Non-number characters,replaced by Blankspace.Numstr=re.sub ('[^0-9.e+-]'," ", Numstr, count=0, flags=Re. IGNORECASE)#~ Remove Incorrect combining-characters for double type.Numstr=re.sub ('[. e+-] (? =\s)'," ", Numstr.strip (), count=0, flags=Re. IGNORECASE) Numstr=re.sub ('[. e+-] (? =\s)'," ", Numstr.strip (), count=0, flags=Re. IGNORECASE) Numstr=re.sub ('[e+-]$'," ", Numstr.strip (), count=0, flags=Re. IGNORECASE) Numstr=re.sub ('[e+-]$'," ", Numstr.strip (), count=0, flags=Re. IGNORECASE)ifLen (Numstr.strip ()) >0:striods=Stringio (Numstr.strip ()) Numarray=numpy.genfromtxt (striods)returnNumarrayif __name__=="__main__": Str= Input ("Enter Your input:"); Donser=str2num (str)Print(Donser)
Python tool string to NumPy floating point group