Reptiles like to use some of the techniques python

1. Regular Expressions

Sometimes the extracted data is not structured, we need to match the required regular show out of data

Learning Link: https://www.cnblogs.com/-chenxs/p/11352172.html , https://www.cnblogs.com/-chenxs/p/11352409.html

2. Cut the data processing sub

Sometimes content or content extracted from the side of both sides of the data string does not need to, or removing spaces to the string, some movement of the character;

We can use string parsing or strip () method to perform a cutting operation on both sides of the content of the string

3.format () method

One way of formatted string

Reptiles for example, we treated url, url generated list when the url to make a value of the parameter changes according to a certain rule when the format will be used to

= URL " http://www.xxx.com/a/b= {} " 
url.format ( . 1) # so =. 1 b 
url.format (I) for I in Range (1,10) # print out b 1-9 nine url =

4. List comprehension

Help us to quickly generate a list that contains a bunch of data

>>>[i+10 for i in range(10)]
[10,11,12,...19]

>>>["10月{}日".format(i) for i in range(1,10)]
["10月1日","10月2日",..."10月9日"]

The dictionary derivations

Help us to quickly generate data dictionary contains a bunch of

>>>{i+10:i for i in range(10)}
{10:0,11:1,12:2,...19:9}
>>>{"a{}".format(i):10 for i in range(3)}
{"a0":10,"a1":10,"a2":10}

6. The ternary operator

Assignment if conditions else another value

---- if the latter condition is satisfied, if put in front of assigning a result, otherwise the results back else assigned to a

a=10 if 2>1 else 20 # a=10

a=10 if 2<1 else 20 # a=20

 

Guess you like

Origin www.cnblogs.com/-chenxs/p/11415860.html