小试python脚本

基础打印

#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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值