Python tool string to NumPy floating point group

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.