Common Methods July

Source: Internet
Author: User

#-*-coding:utf8-*-ImportstringImportRe#------------------Cut, replace string----------------------------------------s ='ASDASDGKD'Dictdata="{' name ': ' Hello ', ' age ':"#String.Replace Replacement S replaces ASD with spaces 1 timesS1 = String.Replace (S,"ASD","", 1)#re.sub Regular ReplacementS2 = re.sub ("^ASD","", s)#Split slice string with the result of listS3 = S.split ("DG")#eval () converts a string to dict if the string is inside a dictionary resultS5 =eval (dictdata)PrintS1,S2,S3,S5#----------------------------------------------------------#-----------------------Testing website encoding-----------------------------------#Detection URL ... CodingRawData = Urllib.urlopen ('http://www.baidu.com/'). Read ()#detect if the connection is successfulResdata = Requests.get ("http://www.baidu.com") d=Chardet.detect (rawdata) e=ResdataPrintDPrinte#----------------------------------------------------------#-------------------The For loop to traverse the build list---------------------------------------#Concise for Loops = ["s={},b={},c={}". Format (str (i), str (i+1), str (i+2)) forIinchRange (20,61,20)]Prints#----------------------------------------------------------#-----------------------time, time stamp-----------------------------------Import TimeImportdatetime#get current time accurate to microsecondsPrintDateTime.Now ()#time Gets the current timestampPrinttime.time ()#datetime, converting a number to a time formats = Datetime.datetime (2012,6,22)#Mktime converts any time to timestamps, using Timetuple to convert time formats into middlewarePrintTime.mktime (S.timetuple ())#Strftime converts timestamps into time formats, using localtime to convert timestamps into middlewaret = time.strftime ('%y-%m-%d', Time.localtime (1467274020.29259))PrintT#----------------------------------------------------------#----------------------URL encoding------------------------------------ImportChardetImportUrllib#change the key value of a dictA = {'a':'%e9%b8%bf%e6%98%9f%e5%b0%94%e5%85%8b','b':{'C':'B1'}}#Pop, delete one of the content keys and values in DictA.pop ('b')#Check E's character encoding e is URL encoded, detected to be ASCIIE = a['a']PrintChardet.detect (e)#unquote, convert URL encoding to Chinese, quote method UTF8 to URL encodings =Urllib.unquote (e)PrintChardet.detect (Urllib.unquote (e))#UTF8PrintS#English#The quote method encodes the UTF8 to URL and detects that it is ASCIIPrintUrllib.quote (s)#URL encodingPrintChardet.detect (Urllib.quote (s))#ASCII#----------------------------------------------------------#--------------two list-> composed of tuple-> into list-> traversal generation Dict------------------URL = ['page={}'. Format (str (i)) forIinchRange ()]val= Range (1,3)#the zip generates a tuple of tuples until the group is not a tuple and then a list of listsURLV =Zip (url,val)#Looping list Generation DictUrldict = Dict ((urls,val) forUrls,valinchurlv)Printurldict#----------------------------------------------------------

Common Methods July

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.