基础打印
#ywyuan.py
import pandas as pd
startline = ">" *2
endline = "<" * 5
undeline = "—" * 10
def ywprint(var:str):
print("Begin" + undeline + startline)
print(var)
print(endline + undeline * 2 + " End \n")
def printRows(desc,rows):
columnDict=[]
for field in desc:
columnDict.append(field[0])
fmtRows=pd.DataFrame(rows,columns=columnDict)
rowCt=len(rows)
columnCt=len(columnDict)
print(undeline * columnCt)
print(fmtRows)
print(undeline * columnCt)
print(f"Print {rowCt} Rows * {columnCt} Columns.")
return fmtRows
文件读写
#ywyuan.py
def readFileData(path:str):
f = open(path, 'r')
file_data = f.read()
f.close()
return file_data
def writeFileData(path:str,content:str):
f = open(path, 'w')
f.write(content)
f.close()
def loadFileRows(path:str):
list = []
for line in open(path, 'r'):
# 去掉空格 \n \r \t
afterLine = line.strip().replace('\n', '').replace('\r', '')
list.append(afterLine)
return list
def wirteFileRows(listData:list,path:str):
f = open(path, 'w')
for line in listData:
f.write(line+'\n')
f.close()
test-file
#taste-file.py
import ywyuan
# writeFile
cusFilePath='/Users/yuan/yw-shell/taste-python/export.txt'
content = "Hello there!\nywyuan!\nO(∩_∩)O哈哈~"
ywyuan.writeFileData(cusFilePath,content)
cc = ywyuan.readFileData(cusFilePath)
ywyuan.ywprint(cc)
# write row by row
listData = [] ## 空列表
listData.append('Google') ## 使用 append() 添加元素
listData.append('Runoob')
listData.append('Fastman')
listData.append('Ywyuan')
ywyuan.ywprint(listData)
ywyuan.wirteFileRows(listData,'/Users/yuan/yw-shell/taste-python/cusout.log')
rowList = ywyuan.loadFileRows('/Users/yuan/clean_data/cusCode.txt')
ywyuan.ywprint(rowList)
rows = ywyuan.loadFileRows('/Users/yuan/yw-shell/taste-python/cusout.log')
ywyuan.ywprint(rows)
http请求
#ywyuan.py
import requests
import json
def getRequest(url):
print("\n-----startRequest--------------->")
r = requests.get(url)
print("GetRequest: "+url)
print("StatusCode: " + str(r.status_code))
print("ResContent: " +r.text)
print("-----endRequest--------------->\n")
return r
def postRequest(url, postdatas, headerData={}):
print("\n-----startRequest--------------->")
r = requests.post(url, data=postdatas, headers=headerData)
print("PostRequest: "+url)
print("StatusCode : " + str(r.status_code))
print("ResContent : " +r.text)
print("-----endRequest--------------->\n")
return r
def postForm(url, formData,headers={}):
# formHeader = {'Content-Type': 'application/x-www-form-urlencoded'}
headers['Content-Type']='application/x-www-form-urlencoded'
return postRequest(url, formData, headers);
def postJson(url, postdatas,headers={}):
headers['Content-Type']='application/json'
jsonData = json.dumps(postdatas)
return postRequest(url, jsonData, headers);
taste-get
#taste-get.py
import ywyuan as yw
# getRequest
url = 'http://abc.xx.com/tool/getProfile'
r = yw.getRequest(url)
print(r.text)
# postFormRequest
formData = {"roomCode": "123", "orderCount": 2}
res = yw.postForm("http://abc.xx.com/room/p", formData,{'id':'ywyuan'});
print(res.text)
# postJsonRequest
data = {"roomCode": "123", "orderCount": 2}
res = yw.postJson("http://abc.xx.com/room/pj", data);
print(res.text)
DB连接
taste-db
#taste-db.py
import pymysql #导入数据操作的库
import ywyuan
import pandas as pd
#连接数据库
dbCon=pymysql.connect(
host='localhost',
# host='127.0.01',
port=3306,
user='haha_user_name',
password='***pwd***',
database='area',
charset='UTF8'
)
#定义游标
cur=dbCon.cursor()
#执行SQL命令
rowCount=cur.execute("select * from country where addtime>'2018-12-01 11:50:00'")
#获取连接对象的描述信息
desc=cur.description
# 一次获取全量
rows = cur.fetchall()
fmtRows = ywyuan.printRows(desc,rows)
# 写入Excel
ywyuan.writeExcel('/Users/yuan/yw-shell/taste-python/my1stexcel',fmtRows)
cur.close()
dbCon.close()
# # 获取单条查询数据(逐行获取)
# results = cur.fetchone()
# print(results)
# # 取多条
# results = cur.fetchmany(2)
# print(results)
# print(rowCount)