Python summarizes common processing functions

Source: Internet
Author: User
Tags cdata


#* * PLASTIC to ASCII!!! **definttoascii (size):Importbinascii A16=hex (int (size)). Lstrip ("0x") forXinchRange (len (A16), 4): A16='0'+A16returnBinascii.b2a_hex (A16)#* * Fill 0!!! **defaddzone (size): A8=Str (size) forXinchRange (len (size)), 8): A8=STR ('0'+A8)returnA8
#* * Filter tags in HTML * *deffilter_tags (HTMLSTR):ImportRe#Filter CDATA FirstRe_cdata=re.compile ('//<!\[cdata\[[^>]*//\]\]>', Re. I)#Match CDATARe_script=re.compile ('<\s*script[^>]*>[^<]*<\s*/\s*script\s*>', Re. I)#ScriptRe_style=re.compile ('<\s*style[^>]*>[^<]*<\s*/\s*style\s*>', Re. I)#styleRe_br=re.compile ('<br\s*?/?>')#Handling line breaksRe_h=re.compile ('</?\w+[^>]*>')#HTML TagsRe_comment=re.compile ('<!--[^>]*-->')#HTML AnnotationsS=re_cdata.sub ("', HTMLSTR)#Remove CDATAS=re_script.sub ("', s)#Remove ScriptS=re_style.sub ("', s)#Remove StyleS=re_br.sub ("', s)#convert BR to line breakS=re_h.sub ("', s)#Remove HTML TagsS=re_comment.sub ("', s)#Remove HTML Annotations    #remove the extra empty linesBlank_line=re.compile ('\n+') s=blank_line.sub ('\ n', s) s=replacecharentity (s)#Replace entity    returns#* * Replace entities in HTML * *defreplacecharentity (HTMLSTR):Importre char_entities={'nbsp':' ',' the':' ',                'LT':'<',' -':'<',                'GT':'>',' +':'>',                'amp':'&',' -':'&',                'quot':'"',' the':'"',} re_charentity=re.compile (R'? (? p<name>\w+);') SZ=Re_charentity.search (HTMLSTR) whilesz:entity=sz.group ()#entity full name, such as &gt;Key=sz.group ('name')#remove &; After entity, such as &gt; for GT        Try: Htmlstr=re_charentity.sub (char_entities[key],htmlstr,1) SZ=Re_charentity.search (HTMLSTR)exceptKeyerror:#Replace with an empty stringHtmlstr=re_charentity.sub ("', htmlstr,1) SZ=Re_charentity.search (HTMLSTR)returnHtmlstr
#* * Determine if SET * *defisset (variable):ifvariable:returnTruereturnFalse#time stamp Turn timedefDate (unixtime, format ='%y-%m-%d%h:%m'):  Importdatetime d=Datetime.datetime.fromtimestamp (unixtime)returnD.strftime (format)
#Download HTTP image return picture namedefdownimg (imgurl):ImportUrllib2ImportOSImportUUID URL=Imgurl Exten=url.split ('.') name="/var/www/html/downimg/"+str (date (), format ='%y%m%d'))+'/'+str (UUID.UUID1 ()) +"."+exten[3] FileD="/var/www/html/downimg/"+str (date (), format ='%y%m%d'))+'/'  if  notOs.path.exists (FileD):#determine if a folder existsos.makedirs (FileD)#note the type to match when saving the file, if the picture you want to save is JPG, the name of the open file must be in JPG format, otherwise an invalid picture will be generated .conn =urllib2.urlopen (URL) F= Open (name,'WB') F.write (Conn.read ()) F.close ()returnname#microsecond Timedefmicrotime ():Import TimeImportMathreturn '%f%d'% MATH.MODF (Time.time ())

Python summarizes common processing functions

Related Article

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.