1. 程式人生 > >python excel操作及網絡編程

python excel操作及網絡編程

請求 處理 控制 讀取excel 關鍵字參數 save 類型 app value

python excel操作

一:excel獲取值操作

1、導入模塊

import xlrd

2、打開Excel文件讀取數據

data = xlrd.open_workbook(‘excelFile.xls‘)

3、使用技巧

獲取一個工作表

table = data.sheets()[0] #通過索引順序獲取 table = data.sheet_by_index(0) #通過索引順序獲取 table = data.sheet_by_name(u‘Sheet1‘)#通過名稱獲取 獲取整行和整列的值(數組)
   table.row_values(i) table.col_values(i) 獲取行數和列數    nrows = table.nrows ncols = table.ncols 循環行列表數據 for i in range(nrows ): print table.row_values(i) 單元格 cell_A1 = table.cell(0,0).value cell_C4 = table.cell(2,3).value 使用行列索引
cell_A1 = table.row(0)[0].value cell_A2 = table.col(1)[0].value 二:excel寫入操作 1、寫入新的excel文件 步驟一:新建一個excel對象 步驟二:添加一個sheet頁 步驟三:sheet頁中寫入多行內容 步驟四:保存excel對象 實例:
import xlwt,xlrd
book = xlwt.Workbook()
# #新建一個excel對象
sheet = book.add_sheet(‘stu‘)
# #添加一個sheet頁
for i in range(len(title)):
#title多長,循環幾次
sheet.write(0,i,title[i])
# #i既是lis的下標,也代表每一列
# #處理表頭
# #寫excel的時候,你保存的文件名必須是xls
for row in range(len(lis)):
#取lis的長度,控制循環次數
id = lis[row][‘id‘]
#因為lis裏面存的是一個字典,lis[row]就代表字典裏面的每個元素,然後字典取
#固定的key就可以了
name = lis[row][‘name‘]
sex = lis[row][‘sex‘]
new_row = row+1#因為循環的時候是從0開始循環的,第0行是表頭,不能寫
#要從第二行開始寫,所以這裏行數要加一
sheet.write(new_row,0,id)
sheet.write(new_row,1,name)
sheet.write(new_row,2,sex)
book.save(‘stu1.xls‘)
2、寫入已存在的excel文件

Python中一般使用xlrd(excel read)來讀取Excel文件,使用xlwt(excel write)來生成Excel文件(可以控制Excel中單元格的格式),需要註意的是,用xlrd讀取excel是不能對其進行操作的:xlrd.open_workbook()方法返回xlrd.Book類型,是只讀的,不能對其進行操作。而xlwt.Workbook()返回的xlwt.Workbook類型的save(filepath)方法可以保存excel文件。

因此對於讀取和生成Excel文件都非常容易處理,但是對於已經存在的Excel文件進行修改就比較麻煩了。不過,還有一個xlutils(依賴於xlrd和xlwt)提供復制excel文件內容和修改文件的功能。

from xlrd import open_workbook
from xlutils.copy import copy
 
rb = open_workbook(‘1.xls‘)
 
#通過sheet_by_index()獲取的sheet沒有write()方法
rs = rb.sheet_by_index(0) 
wb = copy(rb)
 
#通過get_sheet()獲取的sheet有write()方法
ws = wb.get_sheet(0)
ws.write(0, 0, ‘changed!‘) 
wb.save(‘1.xls‘)

python 網絡編程

1、導入模塊

import requests

2、發送get請求

requests.get(url). text # text方式返回的是字符串

requests.get(url).json # text方式返回的是json

payload = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘}

r = requests.get("http://httpbin.org/get", params=payload) # 使用 params 關鍵字參數,以一個字典來提供這些參數

3、發送Post請求

  • res = requests.post(url).json() #請求url中帶參發送POST請求
  • url_set = ‘http://python.nnzhp.cn/set_sties‘
    d = {
    "stie":"hahfsdfsdf",
    "url":"http://www.nnzhp.cn"
    }

res = requests.post(url_set,json=d).json() #請求url不帶參,d為json格式參數

res = requests.post(url_set,data=d).json() #請求url不帶參,d為字典格式參數

  • data = {‘username‘:‘小明‘,"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() # 使用headers參數指定header

  • 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操作及網絡編程