Python 說明

基本

import

1
2
3
4
5
6
7
8
9
10
11
# 一般 import
import turtle
timmy = turtle.Turtle()
# import 內容(簡單使用)
from turtle import Turtle, Screen
# import all ,可能不知出處,避免使用
from turtle import *
from tkinter import *
# module 太長,給予別名
import turtle as t
timmy = t.Turtle()

pass

1
pass

運算元

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
print(f'10 == 10 : {10 == 10}')
print(f'10 != 10 : {10 != 10}')
print(f'10 > 10 : {10 > 10}')
print(f'10 < 10 : {10 < 10}')
print(f'10 == 10 : {10 == 10}')
print(f'10 == 10 : {10 == 10}')
print(f'10 == 10 : {10 == 10}')

print(f'5 & 7 : {5 & 7:x}')
print(f'5 | 7 : {5 | 7:x}')
print(f'5 ^ 7 : {5 ^ 7:x}')
print(f'~ 5 : {~5 :x}')
print(f'5 >> 1 : {5 >> 1:x}')
print(f'5 << 1 : {5 << 1:x}')

x = 7
print(f'x < 5 and x < 10 : {x < 5 and x < 10}')
print(f'x < 5 or x < 4 : {x < 5 or x < 4}')
print(f'not(x < 5 and x < 10) : {not (x < 5 and x < 10)}')

global function and variable

1
2
3
4
5
6
7
8
import os

def main():
global os

STOCK_DB_FILE = 'stockPrice.db'
if os.path.isfile(STOCK_DB_FILE):
os.remove(STOCK_DB_FILE)

條件式

1
2
3
4
5
6
7
8
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")

迴圈

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)

for x in "banana":
print(x)

for x in range(6): # 0,1, ..5
print(x)

for x in range(2, 6): # 2,3, ..5
print(x)

for x in range(2, 30, 3): # 2,5,8 ..29
print(x)

# pass statement to avoid getting an error
for x in [0, 1, 2]:
pass

# while
i = 1
while i < 6:
print(i)
i += 1

function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# comment 
def my_function(fname, lname):
""" function description """
print(fname + " " + lname)
my_function("Emil", "Refsnes")

def my_function(*kids):
print("The youngest child is " + kids[2])
my_function("Emil", "Tobias", "Linus")

# If the number of keyword arguments is unknown, add a double ** before the parameter name:
def my_function(**kid):
print("His last name is " + kid["lname"])
my_function(fname="Tobias", lname="Refsnes")

# return value
def my_function(x):
return 5 * x
print(my_function(3))

變數

  • array VS list
    • array 屬於 Python 模組 numpy 裡的一種數據類型,所包含的所有元素類型都必須相同
    • list 是 Python 內建的數據類型,可以包含不同的元素類型

None(空值)

1
2
if resp is None:
return None

變數與型態

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
money = 100
name = "Robert"
temperature = 25.2
flag = True
print(type(money)) # <class 'int'>
print(type(name)) # <class 'str'>
print(type(temperature)) # <class 'float'>
print(type(flag)) # <class 'bool'>
print('Hi {name} give you {money} dollars, the temperature is {temp}'.format(name=name, money=money, temp=temperature))
# reference glbal variable
def make_coff(coffee, money):
global profile
for item in MENU:
resources[item] -= MENU[choice]["ingredients"][item]
profile += money

array

1
2
3
4
5
6
7
8
9
10
11
12
rticles = []
divs = soup.find_all('div', 'r-ent')
for d in divs:
if d.find('a'):
href = d.find('a')['href']
title = d.find('a').text
author = d.find('div', 'author').text if d.find('div', 'author') else ''
articles.append({
'title': title,
'href': href,
'author': author
})

list

1
2
3
4
5
6
7
8
9
# 生成空 list
info = list()
record = {
'日期': data[0],
'開盤價': data[3],
'收盤價': data[6],
'成交筆數': data[8]
}
info.append(record)

Dictionaries

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
MENU = {
"espresso": {
"ingredients": {
"water": 50,
"coffee": 18,
},
"cost": 1.5,
},
"latte": {
"ingredients": {
"water": 200,
"milk": 150,
"coffee": 24,
},
"cost": 2.5,
},
"cappuccino": {
"ingredients": {
"water": 250,
"milk": 100,
"coffee": 24,
},
"cost": 3.0,
}
}

resources = {
"water": 300,
"milk": 200,
"coffee": 100,
}
# check item
for item in MENU:
print(f'{item}')
# check item
choice = input('What would you like? (espresso/latte/cappuccino):')
if choice in MENU:
print(f'{item}')
else:
print('input error')

IO

input

1
2
3
coin = float(input('    How many quarters?:'))
coin = int(input(' How many quarters?:'))
name = input()

print

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
money = 100
name = "Robert"
temperature = 25.2
print('Hi {name} give you {money} dollars, the temperature is {temp}'.format(name=name, money=money, temp=temperature))

# show by format
print('hex number={:x}'.format(23)) # hex number=17
print(f' money= ${insert_money:.2f}, cost= ${cost}')

# string direct using variable
print(f'I am {name}') # I am Robert

# 運算式
a = 10
b = 200
print(f'a + b ={a + b}') # a + b =210

# print - not change line and direct output
while current_articles:
print('.', end="",flush=True)

# alignemnt position
print(f'{i:02d} {time_tag:16s} {title}')

file I/O

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# access_mode
# r : read
# rb : read binary
# r+ : read and write
# rb+ : read and write(binary)
# w : write
# wb : write binary
# w+ : write and read
# wb+ : write and read(binary)
# a : appending
# ab : appending binary
# a+ : appending and read
# ab+ : appending and read(binary)

# open and write(json)
with open('gossiping.json', 'w', encoding='UTF-8') as file:
# ensure_ascii=False 輸出中文
# sort_keys=True 按 key 順序排列
json.dump(articles, file, indent=2, sort_keys=True, ensure_ascii=False)

# open and write(csv)
import csv
with open('stockThisMonth.csv', 'w', encoding='UTF-8', newline='') as file: # excel 開檔,中文有問題
# with open('stockThisMonth.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['日期', '開盤價', '收盤價', '成交筆數'])
for info in collected_info:
print(info)
writer.writerow([info['日期'], info['開盤價'], info['收盤價'], info['成交筆數']])

# simple open json file
# encoding='utf-8', load 中文正常
f = open(file_name, 'r', encoding='utf-8')
content = f.read()
# print(content)
dcard_shwo_api_title(content)
print("get from file....")
f.close()

Built-in function

round

1
2
3
4
# 4捨5入
x = round(5.76543, 2)
print(x)
33

set() 函数 - 創建一個無序不重複元素集

1
2
3
4
5
6
def get_author_ids(posts, pattern):
ids = set()
for post in posts:
if pattern in post['author']:
ids.add(post['author'])
return ids

csv

1
2
3
4
5
6
7
8
9
10
import csv

# write
# with open('stockThisMonth.csv', 'w', encoding='UTF-8', newline='') as file: # excel 開檔,中文有問題
with open('stockThisMonth.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['日期', '開盤價', '收盤價', '成交筆數'])
for info in collected_info:
print(info)
writer.writerow([info['日期'], info['開盤價'], info['收盤價'], info['成交筆數']])

json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import json

# write dictionary list to a json format file
articles = []
articles.append({
'title': title,
'href': href,
'push_count': push_count,
'author': author
})
with open('gossiping.json', 'w', encoding='UTF-8') as file:
# ensure_ascii=False 輸出中文
# sort_keys=True 按 key 順序排列
json.dump(articles, file, indent=2, sort_keys=True, ensure_ascii=False)

# check json format
def is_json(myjson):
try:
json.loads(myjson)
except ValueError as e:
return False
return True

# load json to variable
parsed = json.loads(content)

# show json
ensure_ascii=False, show 中文
print(json.dumps(parsed, indent=4, ensure_ascii=False))

sglite3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import csv
import sqlite3
import os

def execute_sql(db, sql_cmd):
cursor = db.cursor()
cursor.execute(sql_cmd)
db.commit()

STOCK_DB_FILE = 'stockPrice.db'
if os.path.isfile(STOCK_DB_FILE):
os.remove(STOCK_DB_FILE)

db = sqlite3.connect(STOCK_DB_FILE)
# need add ID for sqlite3
execute_sql(db, f'CREATE TABLE stockPrice (ID INTEGER, 日期 TEXT, 開盤價 INTEGER, 收盤價 INTEGER, 成交筆數 INTEGER)')

with open('stockThisMonth.csv', newline='', encoding="utf-8") as csvfile:
reader = csv.DictReader(csvfile)
print('日期', '開盤價','收盤價','成交筆數')
for info in reader:
print(info['日期'],info['開盤價'],info['收盤價'],info['成交筆數'])
# no add ID's value
command = f'INSERT INTO stockPrice (ID, 日期, 開盤價, 收盤價, 成交筆數) VALUES ("{info["日期"]}", {info["開盤價"]}, {info["收盤價"]}, {info["成交筆數"]})'
print('command=', command)
execute_sql(db, command)

db.close()

os

1
2
3
4
5
import os

STOCK_DB_FILE = 'stockPrice.db'
if os.path.isfile(STOCK_DB_FILE):
os.remove(STOCK_DB_FILE)

timedate

1
2
3
4
5
6
import datetime

def twDateToGlobal(twDate):
year, month, date = twDate.split("/")
print(datetime.date(int(year), int(month), int(date)))
return datetime.date(int(year), int(month), int(date))

time

1
2
3
4
5
import time

current_date = time.strftime('%Y%m%d')
current_year = time.strftime('%Y')
current_month = time.strftime('%m')

re

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import re

# get h1~6
find_text_content_by_reg(soup, 'h[1-6]')
def find_text_content_by_reg(soup, reg_pattern):
for element in soup.find_all(re.compile(reg_pattern)):
print(element.name, element.text.strip())

# get.png
# $ means the tail, the end of the string.
# \. means "."
find_img_source_by_reg(soup, '\.png$')
# .* means any 0~n character
find_img_source_by_reg(soup, 'beginner.*'+'\.png$')
find_img_source_by_reg(soup, 'crawler.*')
def find_img_source_by_reg(soup, reg_pattern):
for img in soup.find_all('img', {'src': re.compile(reg_pattern)}):
# print(img['class'])
print(img['src'])

count_blog_number(soup, 'card\-blog')
def count_blog_number(soup, pattern):
count = len(soup.find_all('div', {'class' : re.compile(pattern)}))
print("Blog count: " + str(count))

quote/unquote

1
2
# 修正中文 show 亂碼
trailer_url = unquote(movie_info.find('div', {'class': 'release_movie_name'}).find('a')['href'], 'utf-8')

Ref