Q1:http Error 403:forbidden
Python often uses the Urllib2.urlopen function to extract Web page source code, but sometimes this function returns: HTTP Error 403:forbidden, which indicates that the source Web site does not allow crawlers to crawl
Workaround: Access disguised as a browser
#!/usr/bin/env python#-*-coding:utf-8-*-import urllib2url = "http://www.google.com/translate_a/t?client=t&sl= Zh-cn&tl=en&q=%e7%94%b7%e5%ad%a9 "#浏览器头headers = {' user-agent ': ' mozilla/5.0 (Windows; U Windows NT 6.1; En-us; rv:1.9.1.6) gecko/20091201 firefox/3.5.6 '}req = urllib2. Request (url=url,headers=headers) data = Urllib2.urlopen (req). Read () Print data
So you can get the source of the Web page ~
Note: If the source code in Chinese is garbled, you can use:
Print Data.decode ("UTF-8")
See http://www.geekcome.com/content-10-3101-1.html
Q2: File Read and write
Read () reads the entire file each time, and it is typically used to place the contents of the file into a string variable, the difference between ReadLine () and. ReadLines () is that the latter reads the entire file at once, like. Read (): ReadLines () Automatically parses the contents of a file into a list of rows that can be used by Python for ... in ... Structure for processing. On the other hand,. ReadLine () reads only one line at a time, usually much slower than. ReadLines (). You should use only if there is not enough memory to read the entire file at once. ReadLine ()
Note: The last character of F.readline () is ' \ n ' When the cursor is positioned to the No. 0 position on the next line, and if the last return ' \ n ' is required to be removed
line = F.readline () line = Line[:-1]
You can also use "string". Strip (' \ n ')
See http://www.cnblogs.com/kaituorensheng/archive/2012/06/05/2537347.html
For on open (' Poem.txt '): Line = F.readline () Print Line
f = open (' Test.txt ', ' R ') #以读方式打开文件for line in F.readlines (): Print Line
>>>> Welcome_str = "welcome You" >>>> welcome_str[0] ' W ' >>> git_list = ["Qiwsir", " GitHub "," IO "]>>> git_list[0] ' Qiwsir '
Q3:list和str转化
The biggest difference between list and STR is that the list can be changed in the same place, and STR will be immutable.
List and STR conversions
Str.split ()
>>> line = "hello.i am qiwsir. Welcome you. " >>> line.split (".") #以英文的句点为分隔符, get list [' Hello ', ' I am qiwsir ', ' Welcome you ', ' '] >>> line.split (".", 1) #这个1, It is expressed in the above: If Maxsplit is given, at the most maxsplit splits was done. [' Hello ', ' I am qiwsir. Welcome you. " >>> name = "Albert Ainstain" #也有可能用空格来做为分隔符 >>> name.split ("") [' Albert ', ' Ainstain '] Join can be said to be split inverse
The string to be sliced from list
List[0][1:] #remove the first character
Q4:list corresponding elements added
Using the ZIP function
Zip is the melted of 2 arrays together
X=[1, 2, 3, 4, 5]
Y=[6, 7, 8, 9, 10]
Zip (x, y) gets the
[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
list12 = [x+y for x, y in Zip (list1,list2)]
l1=[[1,2],[3,4],[5,6]] The corresponding element and
>>> l1=[[1,2],[3,4],[5,6]]
>>> list (Map (Sum,zip (*L1)))
[9, 12]
Zip Reference: http://www.lfyzjck.com/python-zip/
Q5: Judging data types
Import types
Type (x) is types. Inttype # determine if int type
Type (x) is types. StringType #是否string类型
Types. ListType = = Type (text)
Q6: three mesh operator
True_part If condition else false_part
>>> 1 if True else 01>>> 1 if False else 00
Q7:list to Heavy
Http://www.jb51.net/article/55342.htm
ids = [1,4,3,3,4,2,3,4,5,6,1]
News_ids = List (set (IDS))
News_ids.sort (Ids.index)
function pointers for Q8:python
#Icmp----generates some ICMP trafficdef matchicmp (json_data): if json_data[' network ' [' Icmp ']: print ' Generates some ICMP traffic ' func_sets=[matchentropy,matchicmp,matchirc,matchhttp,matchsmtp,matchdomain,matchfile, Matchregister,matchmutex,matchapi]for func in Func_sets: func (Json_data)
#模块中, the function pointer outside the class calls Def hwFunc1 (x): print ("Waleking's Func1") def HWFUNC2 (x): print ("%s"% (x+1)) print (" Waleking ' s Func2 ") funcsets={" func1 ": hwFunc1," Func2 ": hwfunc2}funcsets[" Func1 "] (1)
Q9:typeerror: ' Nonetype ' object is not iterable
Cause: The value returned by the function that was called eventually, and the variable that the return value is assigned to, does not match.
Q10:python uses the elements in the output dictionary in writing order
dict
, key is unordered. On the
dict
When we do iterations, we cannot determine the order of the keys.
If you want to keep the key in order, you can use OrderedDict
:
>>> from collections import OrderedDict>>> d = dict([(‘a‘, 1), (‘b‘, 2), (‘c‘, 3)])>>> d # dict的Key是无序的{‘a‘: 1, ‘c‘: 3, ‘b‘: 2}>>> od = OrderedDict([(‘a‘, 1), (‘b‘, 2), (‘c‘, 3)])>>> od # OrderedDict的Key是有序的OrderedDict([(‘a‘, 1), (‘b‘, 2), (‘c‘, 3)])
Note that OrderedDict
the keys are arranged in the order in which they are inserted, not the key itself:
>>> od = OrderedDict()>>> od[‘z‘] = 1>>> od[‘y‘] = 2>>> od[‘x‘] = 3>>> od.keys() # 按照插入的Key的顺序返回[‘z‘, ‘y‘, ‘x‘]
Reference:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 001411031239400f7181f65f33a4623bc42276a605debf6000
Python is picking up flowers