python鍩虹瀛︿範6-mongodb銆乻ys銆佹帴鍙e紑鍙戙€佹搷浣渆xcel

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python鍩虹瀛︿範6-mongodb銆乻ys銆佹帴鍙e紑鍙戙€佹搷浣渆xcel相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e7%9b%ae%e5%bd%95' title='鐩綍'>鐩綍   鏌ユ壘   dfs   鍚嶇О   url   lin   璇彞   鎵撳紑   ODB   

1       mysql琛ュ厖

cur = conn.cursor(cursor=pymysql.cursors.DictCursor)    #鐩存帴鑾峰彇鐨勬暟鎹浆鎹负瀛楀吀鏍煎紡鐨?/p>

cur.description                   #鐩存帴鑾峰彇鐨勬弿杩颁俊鎭?/p>

fileds = [filed[0] for filed in cur.description]        #鍒楄〃鐢熸垚寮忥紝鑾峰彇鍒扮涓€琛屾墍鏈夌殑瀛楁

 

import pymysql,xlwt
conn = pymysql.connect(host=鈥?.1.1.1鈥?user=鈥榡xz鈥?password=鈥?23456鈥?db=鈥榡xz鈥?port=3306,charset=鈥榰tf8鈥?
# cur = conn.cursor(cursor=pymysql.cursors.DictCursor) #鐩存帴鑾峰彇鐨勬暟鎹浆鎹负瀛楀吀鏍煎紡鐨?br>cur = conn.cursor() #鍐檈xcel鐢ㄩ粯璁ょ殑浜岀淮鏁扮粍鍗冲彲锛屽洜涓虹敤瀛楀吀鐨勮瘽椤哄簭涓嶅浐瀹氾紝鏁版嵁浼氬啓閿?br>cur.execute(鈥榮elect * from app_student limit 10鈥?
print(cur.description) #琛ㄧ殑淇℃伅
fileds = [filed[0] for filed in cur.description] #鍒楄〃鐢熸垚寮忥紝鑾峰彇鍒版墍鏈夌殑瀛楁
print(fileds)
# print(cur.fetchall())
data = list(cur.fetchall())
data .insert(0,fileds) #insert鍙槸鎻掑叆鎸囧畾浣嶇疆鐨勬暟鎹?br>print(data)
book = xlwt.Workbook()
sheet = book.add_sheet(鈥榮heet1鈥?
#鏂规硶涓€锛?br># for index,line_data in enumerate(data):
# for index2,col_data in enumerate(line_data):
# sheet.write(index,index2,col_data)
#涓€涓竴涓崟鍏冩牸鍐欙紝鍏堝啓绗竴琛岀殑姣忎釜鍗曞厓鏍?br>#鍐欐瘡涓€琛岀殑鏃跺€欙紝琛屽彿鏄笉鍙樼殑锛屽垪鍦ㄥ彉

#鏂规硶浜岋細姝ゆ柟娉曢渶瑕佸畾涔変竴涓鍜屼竴涓垪鍘绘帶鍒?br>line = 0 #琛屽彿

for d in data: #鎺у埗琛?br> col = 0 # 鍒楀彿
for col_data in d: #鎺у埗鍒?br> sheet.write(line,col,col_data)
col+=1
line+=1

book.save(鈥榚x.xls鈥?

cur.close()
conn.close()


import pymysql,xlwt
def mydb(sql):
conn = pymysql.connect(host=鈥?.1.1.1鈥?user=鈥榡xz鈥?password=鈥?23456鈥?port=3306,db=鈥榡xz鈥?charset=鈥榰tf8鈥?autocommit=True)
cur = conn.cursor()
cur.execute(sql)
fileds = [filed[0] for filed in cur.description] #鍒楄〃鐢熸垚寮忥紝鑾峰彇鍒扮涓€琛岃〃澶存暟鎹俊鎭?br> data = list(cur.fetchall()) #灏嗚幏鍙栫殑鏁版嵁鐩存帴杞崲鎴愬厓缁?br> data.insert(0,fileds) #灏嗙涓€琛岃幏鍙栧埌鐨勫垪琛ㄦ彃鍏ュ埌data鍒楄〃鐨勭1涓綅缃?br> return data
print(mydb(鈥榮elect * from app_student limit 10鈥?)

def write_excel(excel_name):
book = xlwt.Workbook
sheet = book.add_sheet(鈥榮heet1鈥?
a = 鈥榮elect * from app_student鈥?br> for index,line_data in enumerate(mydb(sql=a)):
for index2,col_data in enumerate(line_data):
sheet.write(index,index2,col_data)
book.save(excel_name)

2       sys妯″潡

import sys

print(sys.argv)     #鎶婅繍琛宲ython鏂囦欢鐨勬椂鍊欙紝浼犲叆鐨勫弬鏁伴兘鏀惧埌argv閲岄潰

 

if len(command)>1:
excel = command[1]
print(鈥樼敤渚嬪凡缁忚繍琛屸€?excel)
else:
print(鈥樿繍琛宲ython鏂囦欢鐨勬椂鍊欙紝闇€瑕佷紶鍏ヤ竴涓敤渚嬪悕绉帮紝渚嬪锛歱ython run_case.py case.xls鈥?

 

瀵煎叆妯″潡鐨勯『搴忥紙閲嶇偣锛?/span>

1銆佷粠褰撳墠鐩綍涓嬫壘python鏂囦欢

2銆佸鏋滃綋鍓嶇洰褰曚笅娌℃湁锛屽垯鍘籶ython鐨勭幆澧冨彉閲忥紙姝ゅ涓嶆槸鎿嶄綔绯荤粺閭h缃殑鐜鍙橀噺锛夐噷闈㈡壘杩欎釜python鏂囦欢锛宲rint(sys.path) #鏌ョ湅python鑷繁鐨勭幆澧冨彉閲?/p>

 

瀵煎叆妯″潡鐨勫疄璐紙閲嶇偣锛?/span>

鎶婅繖涓ā鍧楃殑浠g爜浠庡ご鍒板熬鎵ц涓€娆?/p>

 

 

name = 鈥榟ahaha鈥?/p>

def conn_db(name):

print(鈥榯his is a funcation鈥?name)

print(__name__)

if __name__ == 鈥榑_main__鈥?      #鍦ㄥ叾浠杙ython鏂囦欢涓鍏ヨ妯″潡鏃讹紝瀹冧笉浼氭墽琛宨f __name__ == 鈥榑_main__鈥樹笅闈㈢殑浠g爜

conn_db(鈥榖鏂囦欢鈥?

 

 

sys.path.append(r鈥楧:pythonscriptday7 ools鈥?              #濡傛灉鍒涘缓鐨刾ython鏂囦欢涓嶅湪鐜鍙橀噺涓紝鍙互閫氳繃姝ゆ柟娉曚复鏃惰缃幆澧冨彉閲忎娇鐢?/p>

 

3       mongodb

import pymongo

client = pymongo.MongoClient(host=鈥?18.24.3.40鈥?port=27017)

db = client[鈥榸houyifan鈥榏        #閫夋嫨鏁版嵁搴擄紝濡傛灉涓嶅瓨鍦ㄧ洿鎺ュ垱寤?/p>

collection = db[鈥榮tu_info鈥榏

db.zhouyifan.insert({鈥榥ame鈥?鈥榯est1鈥?鈥榮ex鈥?鈥樼敺鈥?鈥榓ge鈥?18})

db[鈥榮tu_info鈥榏.insert({鈥榰rl鈥?鈥榟ttp://www.jd.com鈥?鈥榯itle鈥?鈥榡d.com鈥?鈥榓ddr鈥?鈥榖eijing鈥榼)   #鎻掑叆

print(list(db[鈥榮tu_info鈥榏.find({鈥榯itle鈥?鈥樼櫨搴︹€榼)))        #鏌ユ壘锛岄渶瑕佽浆鎹㈡垚list鎵嶈兘鎵撳嵃鍑烘潵

for d in db[鈥榮tu_info鈥榏.find({鈥榯itle鈥?鈥樼櫨搴︹€榼):     #浣跨敤寰幆鎵撳嵃鍑烘潵鐨勬槸瀛楀吀

     print(d)

 

collection.delete_one({鈥榯itle鈥?鈥樼櫨搴︹€榼)       #濡傛灉鏈夊鏉$殑璇濓紝鍙細鍒犻櫎涓€鏉?/p>

collection.delete_many()            #浼氬垹闄ゅ鏉?/p>

print(collection)

 

collection.update({鈥榰rl鈥?鈥榟ttp://www.jd.com鈥?鈥榯itle鈥?鈥榳ww.jd.com鈥榼,{鈥榰rl鈥?鈥榟ttp://www.jd.com鈥?鈥榯itle鈥?鈥榳ww.jd.com鈥?鈥榓ddr鈥?鈥樹害搴勨€榼)    #鏇存敼

 

4       鎺ュ彛寮€鍙?/h1>

鎺ュ彛寮€鍙戠殑浣滅敤锛?/p>

1銆?nbsp; 鐭ラ亾鎺ュ彛鏄€庝箞寮€鍙戠殑

2銆?nbsp; 閬垮厤鍒汉鎿嶄綔浣犵殑鏁版嵁搴?/p>

3銆?nbsp; mock鏈嶅姟锛堝嵆妯℃嫙涓€涓湇鍔★紝淇濊瘉娴嬭瘯娴佺▼鑳藉椤哄埄杩涜锛?/p>

 

import sys
print(sys.path) #鏌ョ湅python鑷繁鐨勭幆澧冨彉閲?/p>

 

python鐜鍙橀噺璁剧疆濡備笅锛?/p>

鏂规硶涓€锛?/p>

蹇€熷皢鏁翠釜鐩綍璁剧疆python鐨勭幆澧冨彉閲忥紙濡備笅鏂规硶鍙兘鍦╬ycharm涓ソ鐢紝濡傛灉灏嗕唬鐮佹嫹璐濈粰鍏朵粬浜轰娇鐢紝鍒欒缃殑鐜鍙橀噺鏄笉鐢熸晥鐨勶級锛?/p>

 鎶€鏈垎浜浘鐗? src=

 

绗簩绉嶈缃幆澧冨彉閲忕殑鏂规硶濡備笅锛?/p>

import sys,os

base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))     #鐜鍙橀噺璁剧疆蹇呴』鍦ㄥ鍏ヨ嚜宸卞啓鐨勬ā鍧椾箣鍓嶏紝鑷姩鑾峰彇褰撳墠鏂囦欢鍚戜笂涓ょ骇鏂囦欢澶圭殑璺緞

sys.path.insert(0,base_path)        #灏嗗姩鎬佽幏鍙栧埌鐨勭幆澧冨彉閲忔彃鍏ュ埌鏈€鍓嶉潰锛岃繖鏍峰鍏ユā鍧楁洿蹇?/p>

 

import sys,os
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #__file__鏄綋鍓嶈繖涓猵ython鏂囦欢鐨勭粷瀵硅矾寰?
# os.path.abspath(__file__)閫氳繃abspath鏂规硶灏嗚幏鍙栫殑璺緞鍒嗛殧绗﹁繘琛岃浆鎹?br>#鍐嶉€氳繃涓や釜dirname鑾峰彇鍒扮埗鐩綍
print(base_path)
sys.path.insert(0,base_path)

 

涓嬮潰鏄粠鑷繁鍐欑殑妯″潡瀵煎叆鎺ュ彛妯″潡锛?/p>

from lib.interface import server

from conf.setting import SERVER_PORT

server.run(host=鈥?.0.0.0鈥?port=SERVER_PORT,debug=True)      #璁剧疆host=0.0.0.0灏卞彲浠ュ湪鍚屼竴灞€鍩熺綉鍐呭叾浠栫數鑴戣兘璁块棶浜嗭紝濡傛灉涓嶆寚瀹歨ost锛屽垯鍙兘鏈満璁块棶

 

 

name = 鈥榟ahaha鈥?br>def conn_db(name):
print(鈥榯his is a funcation鈥?name)

print(鈥榑_name__鈥?__name__)
if __name__ == 鈥榑_main__鈥? #鍦ㄥ叾浠杙ython鏂囦欢涓鍏ヨ妯″潡鏃讹紝瀹冧笉浼氭墽琛宨f __name__ == 鈥榑_main__鈥樹笅闈㈢殑浠g爜
conn_db(鈥榖鏂囦欢鈥?

 

鎺ュ彛寮€鍙戠ず渚嬶細

import flask
import json
import pymysql
import hashlib
server = flask.Flask(__name__) #鎶婂綋鍓嶇殑python鏂囦欢褰撳仛涓€涓湇鍔★紝__name__琛ㄧずmain

def my_db(sql,port=3306,charset=鈥榰tf8鈥?:
ip, user, passwd, db = 鈥?.1.1.1鈥?鈥榡xz鈥?鈥?23456鈥?鈥榡xz鈥?br> conn = pymysql.connect(host=ip,user=user,
password=passwd,db=db,
port=port,charset=charset,autocommit=True)
cur = conn.cursor()
sql=sql.strip()
cur.execute(sql)
sqlstart = sql[:6].lower() #鍙杝ql鐨勫紑澶?浣嶏紝杞崲涓哄皬鍐?br> if sqlstart.startswith(鈥榮elect鈥? or sqlstart.startswith(鈥榠nsert鈥?: #鍒ゆ柇鏄痵elec鎴杝how鐨勮鍙ヨ幏鍙栧搴旂粨鏋?br> data = cur.fetchall()
else: #鍔爀lse鏄负浜嗕笅闈㈢殑return涓嶆姤閿?br> data = 鈥榦k鈥?br> cur.close()
conn.close()
return data

def my_md5(s):
m = hashlib.md5(s.encode())
return m.hexdigest()

@server.route(鈥?login鈥?methods=[鈥榞et鈥?鈥榩ost鈥榏)
def login():
username = flask.request.values.get(鈥榰sername鈥? #鏄粠瀹㈡埛绔彂閫佽繃鏉ョ殑鏁版嵁
pwd = flask.request.values.get(鈥榩assword鈥?
# flask.request.cookies.get(鈥榮sss鈥? #鑾峰彇cookie
# flask.request.headers.get(鈥榯ttt鈥? #鑾峰彇headers
# json_data = flask.request.json #鑾峰彇鍏ュ弬鏄痡son绫诲瀷鐨勬暟鎹?br> if username==鈥榯est1鈥榓nd pwd==鈥?23456鈥?
res = {"code":1000,"msg":"鐧诲綍鎴愬姛","sing":"dfsdfsdfsdfsdf"}
else:
res = {"code":2000,"msg":"璐﹀彿/瀵嗙爜閿欒"}
return json.dumps(res, ensure_ascii=False)

@server.route(鈥?register鈥?methods=[鈥榩ost鈥榏)
def reg():
username = flask.request.values.get(鈥榰sername鈥?
pwd = flask.request.values.get(鈥榩assword鈥?
cpwd = flask.request.values.get(鈥榗password鈥?

if username and pwd and cpwd:
sql1 = 鈥榮elect * from nhy where name="%s";鈥?%username
sql_res = my_db(sql1)
if sql_res:
res = {"code":2001,"msg":"璇ョ敤鎴峰凡娉ㄥ唽"}
else:
if pwd==cpwd:
md_pwd = my_md5(pwd)
sql2 = 鈥榠nsert into nhy (name,pwd) value("%s","%s");鈥?%(username,md_pwd)
sql_res = my_db(sql2)
res = {"code": 1000, "msg": "娉ㄥ唽鎴愬姛"}
else:
res = {"code": 2002, "msg": "涓ゆ瀵嗙爜杈撳叆涓嶇浉鍚?}
else:
res = {"code":2002,"msg":"蹇呭~鍙傛暟鏈~锛岃鏌ョ湅鎺ュ彛鏂囨。锛?}
return json.dumps(res, ensure_ascii=False)

server.run(port=8989,debug=True) #鍚姩鏈嶅姟锛屾椤归渶瑕佹敞鎰忥紝鍚姩璇ユ湇鍔′箣鍚庡鏋滀慨鏀硅剼鏈啀娆¤繍琛岀洿鎺ラ噸鍚嵆鍙紝涓嶈兘鍐嶆鐐瑰嚮鍙抽敭杩愯锛屽惁鍒欎細鎶ユ帴鍙e啿绐侊紝鍥犱负鍘熸湁杩愯鐨勬湇鍔℃湭鍋滄

鎶€鏈垎浜浘鐗? src=

 

 

5       淇敼excel

import xlrd

from xlutils import copy        #淇敼excel闇€瑕佸鍏ヨ妯″潡

book1 = xlrd.open_workbook(鈥榚x.xls鈥?        #1銆佹墦寮€鍘熸潵鐨別xcel

new_book = copy.copy(book1)                 #2銆佹嫹璐濅竴涓柊鐨別xcel

sheet = new_book.get_sheet(0)              #3銆佽幏鍙栫涓€涓猻heet椤?/p>

sheet.write(1,3,鈥?8鈥?

sheet.write(1,1,鈥榵iaobai鈥?

new_book.save(鈥榚x.xls鈥?

 

6       鎿嶄綔excel

#xlwt 鍙兘鍐檈xcel

#xlrd 鍙兘璇籩xcel

import xlrd

book = xlrd.open_workbook(鈥榚x.xls鈥?

sheet = book.sheet_by_index(0)      #閫氳繃sheet绱㈠紩鑾峰彇sheet

# sheet = book.sheet_by_name(鈥榮heet1鈥?      #閫氳繃sheet鍚嶇О鑾峰彇sheet

print(book.nsheets)     #鑾峰彇鍒癳xcel涓€诲叡鏈夊灏戜釜sheet椤?/p>

 

print(sheet.cell(0,0).value)    #鎸囧畾琛屽拰鍒楄幏鍙栨煇涓崟鍏冩牸閲岀殑鍐呭

print(sheet.cell(0,1).value)

 

print(sheet.row_values(0))      #鑾峰彇鏌愪竴琛岀殑鏁版嵁

print(sheet.nrows)      #鑾峰彇excel琛ㄤ腑鎬诲叡鏈夊灏戣






































以上是关于python鍩虹瀛︿範6-mongodb銆乻ys銆佹帴鍙e紑鍙戙€佹搷浣渆xcel的主要内容,如果未能解决你的问题,请参考以下文章

python瀛︿範--bisect妯″潡

python瀛︿範绡?5 - 瀛楀吀 闆嗗悎

Python瀛︿範涔嬭矾鈥?018/7/11

Python瀛︿範涔嬭緭鍏ュ拰杈撳嚭

python dlib瀛︿範锛堜簲锛夛細姣斿浜鸿劯

python瀛︿範涔嬫姄鍙栫尗鐪肩數褰盩op100姒滃崟