Category: Python

Python: Check if String Contains Substring

Ref: https://stackabuse.com/python-check-if-string-contains-substring/ fullstring = “StackAbuse” substring = “tack” if substring in fullstring:    print(“Found!”) else:    print(“Not found!”)   Read More

Python : Split by Character and Get front part

Ref: https://bobbyhadz.com/blog/python-split-string-and-get-last-element   my_str = ‘bobby_hadz_com’ first = my_str.split(‘_’, 1)[0] print(first) # 👉️ ‘bobby’   Read More

Python : Data to Text (.txt)

Ref: https://www.w3schools.com/python/python_file_write.asp   To write to an existing file, you must add a parameter to the open() function: “a” – Append – will append to the end of the file “w” – Write – will overwrite any existing content   f = open(“demofile2.txt”, “a“) f.write(“Now the file has more content!”) f.close()   f = open(“demofile3.txt”, “w“) f.write(“Woops! I have deleted the content!”) f.close() Read More

Python : Unicode to 中文

Ref: https://ithelp.ithome.com.tw/articles/10187778   import sys print(sys.getdefaultencoding()) # 打印出目前系統字符編碼     s = ‘你好’ s_to_unicode = s.decode(encoding=‘utf-8’) # 要告訴decode原本的編碼是哪種 print(s_to_unicode) Read More

Python 爬蟲 : Capture HTML CODE 取得網頁HTML Code

Ref: https://www.guru99.com/accessing-internet-data-with-python.html Python Internet Access using Urllib.Request and urlopen() Python 3 Example # # read the data from the URL and print it # import urllib.request # open a connection to a URL using urllib webUrl = urllib.request.urlopen(‘https://www.youtube.com/user/guru99com’) #get the result code and print it print (“result code: ” + str(webUrl.getcode())) # read the data Read More

line bot 發佈文字訊息(push text message) 流程

可參考 https://learn.markteaching.com/%E3%80%90chatbot-%E6%95%99%E5%AD%B8%E3%80%91-line%E6%A9%9F%E5%99%A8%E4%BA%BA-%E7%99%BC%E4%BD%88%E6%96%87%E5%AD%97%E8%A8%8A%E6%81%AFpush-text-message/   首先需要在Line 控制台取得 User id 接著再程式碼內新增一個方法(method) 最後就是發佈文字訊息就能搞定啦!   下面示範的是當user 對line BOT 說 “GetUID”, line BOT 就會回傳其UID, 再主動發一個MESSAGE 給該USER~ if event.message.text == “GetUID”: uid = event.source.user_id login_status = “not-member” line_bot_api.reply_message(event.reply_token, TextSendMessage(text=uid)) line_bot_api.push_message(uid, TextSendMessage(text=login_status)) Read More

‘staticfiles’ is not a registered tag library. Must be one of

可參考 https://stackoverflow.com/questions/55929472/django-templatesyntaxerror-staticfiles-is-not-a-registered-tag-library Read More

ImportError: cannot import name ‘url’ from ‘django.conf.urls’ (在Django 4 用舊版本指令的後果)

可參考 https://www.youtube.com/watch?v=cRrShdhqyuM 開啟 urls.py : from django.urls import path, include, re_path from django.urls import re_path as url     Read More

How to Use venv to Create Virtual Environments in Python 3 (建立虛擬環境)

python – m venv [name_by_you] Read More

“’django-admin’ is not recognized as an internal or external command,” 解決方法

python -m venv venv venv\Scripts\activate pip install django django-admin startproject mysite Read More

List

List 入面的東西可以不同Type List 入面亦可以有另一個 List List 入面不同的 List 可以有不同的 dimension L=[1,”1″,2.5,True] L[0] L[1] L[2] L[3] L[4]   #以下有是指 L1 有3個 List (List 中 List) ,但count L1 的length 只會有3個 items L1=[[1,”1″,2.5,True,2,3],[4.1,”ABC”,4],[7,8,9,10]] # 取得 L1 的 length len(L1) # 取得 L1 內第0個元素的  length len(L1[0]) # 取得 L1 內第1個元素的  length len(L1[1]) # 取得 L1 內第2個元素的  length len(L1[2]) 以下會示範取得 Read More

Filtering by postion : first one (第一個), 第x個 (某一個), last one (最後一個), 單數/雙數

在開始之前,先學會COUNT 一個字串的長度 function: len() s=”1234567″ len(s) expected result: 7   接下來,要著意python 第一個值是 0 ; 最後一個值是 -1 s=”1234567″ # Python initial position is 0 s[0] 所以first one  : s[0] # Python last position is -1 s[-1] 所以last one  : s[-1]   再來就是抽某一個範圍的數值 s=”1234567″ s[x:n] —– 其中x, n為數字; 代表第x個位置開始抽,抽到第 N-1 個位置 ; 例子: # 由第x個位置開始抽,抽到第 N-1 個位置 Read More

1st Python : Print

# print() is a function in python print(“hello”)   值得留意的是,如果將 “hello” 設定為variable, 則不用print 的情況下,在Jupyter 都可以直接Run,例子: c=”Python is cool” c Expected result: Python is cool   Read More

設置Django (Windows版Anaconda)

安裝虛擬環境 開啟 Anaconda 的 CMD.exe Promopt pip install virtualenv pip install virtualenvwrapper   安裝Anaconda 開啟 Anaconda 的 CMD.exe Promopt conda install -c anaconda django Ref: https://anaconda.org/anaconda/django   Anaconda Django “python manage.py runserver” Error (環境變數設定)   建置Django 開啟 Anaconda 的 CMD.exe Promopt 進入Python Env 的文件夾 (e.g D:\Python\envs) D: cd Python\envs dir conda create –name django2 django Read More

Anaconda Django “python manage.py runserver” Error (環境變數設定)

如果要在Windows 版Anaconda 中用 Django, 就應該先做環境變數設定,  否則您可能會出現Error 方法是先locate Anaconda 安裝在那, 如果Anaconda 本身安裝在D:\Python, 就入去看看會吾會有python37 (代表python 3.7) 的檔案 右按”我的電腦”>>內容>>進階系統設定>>進階>>環境變數 新增>>變數名稱: “path” ; 變數值: “D:\Python;D:\Python\python37”   Read More

檢查某”字”是否在陣列, 字典, 字串

Example 1 arr = [1,2,3] print (1 in arr) – 預期True print (9 in arr) – 預期False   Example 2 arr = [[1,2,3],[4,5,6]] print (1 in arr) – 預期False print (1 in arr[0]) – 預期True print (4 in arr[0]) – 預期False print (4 in arr[1]) – 預期True   Example 3 x = {“name”:”Chan Tai Man”, Read More

Python 陣列應用 (array)

建立一個3維陣列 arr = [[1,2,3],[4,5,6],[7,8,9]] 列出整個陣列 print(arr) 列出某維陣列的第X個元素 print (arr[0][0])  – 預期得到”1″ print (arr[1][0]) – 預期得到”4″ print (arr[0][1]*arr[1][1]) – 預期得到”2*5 = 10″ 將某陣列內數值加總 print (sum(arr[0])) print (sum(arr[1])) Read More

Python 重覆運算

Examples a = 0 a += 5 print (a) a = 0 a -= 5 print (a) a = 5 a *= 2 print (a) a = 5 a /= 2 print (a) a = 5 a %= 2 print (a) a = 2 a **= 3 print (a) Read More

Python 中的字典函數

建立字典 – 鍵值對 x = {“name“:”Chan Tai Man“} print(x[“name”]) 加入/更改鍵值對 x[“gender“] = “Male” print (x) 移除鍵值對 x.pop(‘gender’) print (x) Read More

Python 中的清單 (List)

建立清單 x = [“abc”, 22, None] print (x[2]) print (x[-1]) 上述兩個print 既result 都會預計係 None, 後者方法指list 出由尾數既第1個 (p.S 如果要show “abc”, 要用”print (x[0])”)   加入新元素到清單 x = [“abc”, 22, None] print (x) x.append(“Love”) print(x)   移除第X個清單中的元素 x = [“abc”, 22, None] print (x) x.pop(1) print(x) 所以 “22” 會從清單中被移除   將清單中的元素順序及反序例出 x = [3, 1 , 9, 222, Read More

1 2
wpChatIcon