入坑python 自己寫的小工具,紀念一下


這個程序的功能是可以從表格中讀取某一列數據,傳到IDs 這一個參數里,然后在url中獲取相應的請求值,並打印

import urllib.request
import json
import xlrd

def read_excel():
    # 打開文件
    workbook = xlrd.open_workbook(r'G:\Python\est-export.xls')
    # 獲取所有sheet
    print(workbook.sheet_names())# [u'sheet1', u'sheet2']
    #獲取sheet2
    sheet2_name = workbook.sheet_names()[0]
    print(sheet2_name)
    # 根據sheet索引或者名稱獲取sheet內容
    sheet2 = workbook.sheet_by_name('Sheet1')
    # sheet的名稱,行數,列數
    print(sheet2.name,sheet2.nrows,sheet2.ncols)
    rows = sheet2.row_values(3) # 獲取第四行內容
    cols = sheet2.col_values(2) # 獲取第三列內容
    print(rows)
    print(",".join(cols))
    return ",".join(cols)#去掉單引號


def getdata():
    IDs = read_excel()
    url = "http://XXX/ids?ids="+IDs
    response = urllib.request.urlopen(url).read()
    data = response.decode('utf-8')
    reps = json.loads(data)
    # print(data)
    if reps["statuscode"] != 0:
        print("查詢失敗")
    nickNameList = []
    for usersData in reps["usersdetailinfo"]:
        username = usersData["username"]
        name = usersData["nickname"]
        nickNameList.append({"name": name, "username": username})

    return nickNameList

result = getdata()
for nickName in result:
    print("用戶姓名等信息,%s, %s " % (nickName["username"], nickName["name"]))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM