Python Excel operations
One: Excel get Value action
1. Import Module
Import xlrd
2. Open Excel file to read data
data = Xlrd.open_workbook (' Excelfile.xls ')
3, the use of skills
Get a worksheet
Table = data.sheets () [0] #通过索引顺序GetTable = Data.sheet_by_index (0) #通过索引顺序GetTable = data.sheet_by_name (U ' Sheet1 ') #通过名称获取
get the values (arrays) for the entire row and the entire columnTable.row_values (i) table.col_values (i)
get the number of rows and columnsnrows = table.nrows Ncols = Table.ncols
cyclic row and column table dataFor I in Range (nrows): Print table.row_values (i)
Cell CellCELL_A1 = Table.cell (0,0). Value cell_c4 = Table.cell (2,3). Value
using row and column indexesCELL_A1 = Table.row (0) [0].value cell_a2 = Table.col (1) [0].value II
: Excel write Operation
1. Write a new Excel file
Step One: Create a new Excel object
Step Two: Add a sheet page
Step Three: Write multiple lines of content on the sheet page
Step four: Save the Excel object
Example:
Import xlwt,xlrd
Book = XLWT. Workbook ()
# #新建一个excel对象
Sheet = book.add_sheet (' stu ')
# #添加一个sheet页
For I in range (len (title)):
#title多长, loop a few times
Sheet.write (0,i,title[i])
# #i既是lis的下标, also on behalf of each column
# #处理表头
# #写excel的时候, the file name you saved must be XLS
For row in range (len (LIS)):
#取lis的长度, Control cycle times
id = lis[row][' id ']
#因为lis里面存的是一个字典, Lis[row] represents every element in the dictionary, and then the dictionary takes
#固定的key就可以了
name = lis[row][' name ']
sex = lis[row][' sex ']
New_row = row+1# Because the loop starts at 0, the No. 0 row is the table header and cannot be written
#要从第二行开始写, so the number of rows here is to add a
Sheet.write (New_row,0,id)
Sheet.write (New_row,1,name)
Sheet.write (New_row,2,sex)
Book.save (' Stu1.xls ')
2. Write to an existing Excel file
In Python, you typically use XLRD (Excel read) to read an Excel file, use XLWT (Excel write) to generate an Excel file (you can control the formatting of cells in Excel), and be aware that Reading Excel with XLRD cannot operate on it: the Xlrd.open_workbook () method returns XLRD. Book type, which is read-only and cannot be manipulated. and XLWT. Workbook () returns the XLWT. The workbook type of Save (filepath) method can save an Excel file.
So it's easy to handle reading and generating Excel files, but making changes to an existing Excel file can be tricky. However, there is also a xlutils (dependent on xlrd and XLWT) that provides the ability to copy the contents of an Excel file and modify the file.
From XLRD import open_workbookfrom xlutils.copy import Copy RB = Open_workbook (' 1.xls ') #通过sheet_by_index () Gets the sheet without the Write () Method rs = rb.sheet_by_index (0) WB = Copy (RB) #通过get_sheet () Gets the sheet with the Write () method ws = Wb.get_sheet (0) Ws.write (0, 0, ' changed! ') wb.save (' 1.xls ')
Python Network programming
1. Import Module
Import requests
2. Send a GET request
Requests.get (URL). Text # text Returns a string
Requests.get (URL). JSON # Text method returns the JSON
payload = { ' Key1 ' : ' value1 ' ' Key2 ' : ' value2 ' }
< Span class= "p" > r = requests. Get ( "Http://httpbin.org/get" params =payload) # use params keyword parameters to provide these parameters in a dictionary
< Span class= "p" > < Span class= "p" > 3, send POST request
res = Requests.post (url_set,json=d). JSON () #请求url不带参, D is a json format parameter
res = Requests.post (url_set,data=d). JSON () #请求url不带参, D is a dictionary format parameter
- data = {' username ': ' xiaoming ', ' Money ': 8888}
Cookie = {' token1111 ': ' Ajajja '}
res = Requests.post (Cookie_url,data=data,cookies=cookie). JSON () #使用cookies参数指定cookie
- Head_url = ' Http://api.nnzhp.cn/getuser2 '
data = {' UserID ': 1}
Header = {' Content-type ': ' Application/json '}
res = requests.post (URL, headers=header). JSON () # Specify the header with the headers parameter
- Up_url = ' Http://python.nnzhp.cn/upload '
File = {' file_name ': Open (' aaa.py ')}
res = Requests.post (up_url,files=file). Text #使用files参数指定file
Python Excel operations and network programming