
點擊上方"藍字"關注我們
Python大數據分析
記錄? ?分享? ?成長
用python寫小腳本是一件好玩的事情,因為不是個大活兒,而且能解決眼邊前十分繁瑣的事情,這種輕松且便宜的代碼頗受人民群眾的歡迎~有點生活小妙招的意味

大家較為熟知的腳本是用python來做爬蟲、搶票、簽到、自動回復機器人、批量處理文件等,這些比較常規,還有些較復雜的,比如做物品識別、語義分析、圖像處理等,只要你有需求場景,總會想到辦法寫個腳本去處理它。
github上有個python項目,里面提供了幾百個(可能上千)小腳本,涉及到算法、文件、文本、圖像、視頻、音樂、爬蟲、郵件、可視化、系統、下載等各種常用場景的處理腳本。
項目地址:https://github.com/geekcomputers/Python
這個項目不是什么牛逼的大程序,而是作者在日常工作和python學習過程中積累的腳本,一個腳本解決一個問題。獲得1萬9的贊,說明頗有群眾基礎。

作者在介紹中所說,他并非專業程序員,而是為了解決問題、提高效率寫了這些代碼。我也是鼓勵初學者可以先按照這種模式來學習編程,從解決問題的角度來寫代碼,把python當作一把錘子,不斷找釘子。

分享其中幾個腳本:
1、檢查主目錄中是否存在某文件夾,若不存在則創建文件
#?Description???:?Checks?to?see?if?a?directory?exists?in?the?users?home?directory,?if?not?then?create?it
import?os??#?Import?the?OS?module
MESSAGE?=?'The?directory?already?exists.'
TESTDIR?=?'testdir'
try:
????home?=?os.path.expanduser("~")??#?Set?the?variable?home?by?expanding?the?user's?set?home?directory
????print(home)??#?Print?the?location
????if?not?os.path.exists(os.path.join(home,?TESTDIR)):??#?os.path.join()?for?making?a?full?path?safely
????????os.makedirs(os.path.join(home,?TESTDIR))??#?If?not?create?the?directory,?inside?their?home?directory
????else:
????????print(MESSAGE)
except?Exception?as?e:
????print(e)
2、打印圖片分辨率
def?jpeg_res(filename):
???""""This?function?prints?the?resolution?of?the?jpeg?image?file?passed?into?it"""
???#?open?image?for?reading?in?binary?mode
???with?open(filename,'rb')?as?img_file:
???????#?height?of?image?(in?2?bytes)?is?at?164th?position
???????img_file.seek(163)
???????#?read?the?2?bytes
???????a?=?img_file.read(2)
???????#?calculate?height
???????height?=?(a[0]?<8)?+?a[1]
???????#?next?2?bytes?is?width
???????a?=?img_file.read(2)
???????#?calculate?width
???????width?=?(a[0]?<8)?+?a[1]
???print("The?resolution?of?the?image?is",width,"x",height)
jpeg_res("img1.jpg")
3、連接MySQL數據庫
import?mysql.connector
#?MySQl?databses?details
mydb?=?mysql.connector.connect(
????host="0.0.0.0",
????user="root",
????passwd="",
????database="db_name"
)
mycursor?=?mydb.cursor()
#?Execute?SQL?Query?=>>>>?mycursor.execute("SQL?Query")
mycursor.execute("SELECT?column?FROM?table")
myresult?=?mycursor.fetchall()
for?x?in?myresult:
????print(x)
4、PDF轉音頻
import?pyttsx3
import?pyPDF2
book?=?open('book.pdf','rb')
pdfreader?=?pyPDF2.PdfFileReader(book)
pages?=?pdfreader.numPages
print(pages)
speaker?=?pyttsx3.init()
page=?pdfreader.getpage(7)
text?=?page.extractText()
speaker.say(text)
speaker.runAndWait()
· 往期精選 ·18個最高效的Python爬蟲框架,你用過幾個?
2在模仿中精進數據可視化05:疫情期間市值增長top25公司
3關于學習SQL的五個常見問題?
Python大數據分析
data creates?value
掃碼關注我們