文章目錄
一、TypeError: can only concatenate str (not “int”) to str
報錯代碼如下:
r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
print("status_code ==> "+r.status_code)
原因:int類型和str類型不兼容,修改即可。
print("status_code ==> "+str(r.status_code))
二、取消Pycharm語法檢查
關閉拼寫檢查:file --> settings --> Inspections --> Spelling --> 取消勾選Typo
關閉代碼風格檢查:file --> settings --> Inspections --> Python --> 取消勾選PEP 8檢查
三、if__name__=='__main__'
https://blog.csdn.net/anshuai_aw1/article/details/82344884
if __name__ == '__main__'
的意思是:當.py文件被直接運行時,if __name__ == '__main__'
之下的代碼塊將被運行;當.py文件以模塊形式被導入時,if __name__ == '__main__'
之下的代碼塊不被運行。
四、如何判斷值爲空
參考:https://www.cnblogs.com/mikeluwen/p/7422022.html
代碼中經常會有變量是否爲None的判斷,有三種主要的寫法:
if x is not None # 1
if not x # 2
if not x is None # 3
使用if not x這種寫法的前提是:必須清楚x等於None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元組()時對你的判斷沒有影響纔行。if x is not None
是最好的寫法,清晰,不會出現錯誤,以後堅持使用這種寫法。
foo is None 和 foo == None的區別
如果比較相同的對象實例,is總是返回True 而 == 最終取決於 “eq()”
==
只要相等,is
表明要一樣,同一個對象
五、通過第三方服務器發送郵件
登錄qq郵箱,左上角點擊設置,在賬戶一欄,開啓POP3/SMTP服務。
https://www.runoob.com/python3/python3-smtp.html
六、Windows創建定時任務執行Python腳本
https://blog.csdn.net/u012849872/article/details/82719372
七、格式化時間的方式
https://www.runoob.com/python3/python3-date-time.html
#!/usr/bin/python3
import time
# 格式化成2016-03-20 11:45:39形式
print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# 格式化成Sat Mar 28 22:24:24 2016形式
print (time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
# 將格式字符串轉換爲時間戳
a = "Sat Mar 28 22:24:24 2016"
print (time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")))
# 輸出結果
2020-06-06 21:44:05
Sat Jun 06 21:44:05 2020
1459175064.0