0%

今天有想在這裡分享 Regular Expression,這也是大家不想碰到的,可就是一個很復雜的就越耗用。
我也沒有很勵害,但之前開發自動化,用很多。因此想藉由這編文章來寫下我所寫道的筆記和參考很多教學。

在介紹之前我們在 python 都會用到 re 或是 rex ,但在這我只會分享用 re,也就是要 import re,不然不能用。

常用的 req module

Module 說明
match Determine if the RE matches at the beginning of the string.
search Scan through a string, looking for any location where this RE matches
findall Find all substrings where the RE matches, and returns them as a list.
finditer Find all substrings where the RE matches, and returns them as an iterator.

compile()

1
2
3
4
5
6
import re
text ='https://chenchih.page , Author is ChenChih'
pattern=re.compile('Chenchih',re.I)
print(pattern.match(text)) #None
print(pattern.search(text).group()) #chenchih
print(pattern.findall(text)) #['chenchih', 'ChenChih']

output:

None
cheenchih > > ['chenchih', 'ChenChih']

match()

This function matches a regular expression pattern at the beginning of a string. If the pattern is found, it returns a match object, otherwise, it returns None.

1
2
3
4
5
import re
teststring="123abc456789abc123ABC"
pattern=re.compile(r"abc")
match=pattern.match(teststring)
print(match)
Read more »

我想分享一些基本的DB(MYSQL)語法。之前面試有考不管是在Amazon,或Microsoft 都有考過我DB。我那時有準備筆記,但後來就不知跑哪。今天想在這篇寫關於SQL基本指令,有機會再寫高階的語法。

我住前是用MYSQL的DB軟體,你們可以選用其他的如MogoDB, Microsoft SQL server,orlacle DB等等。基本上語法都差不多,我猜的

  • Table: employee:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    +----+-------+--------+--------------+
    | id | Name | Salary | DepartmentID |
    +----+-------+--------+--------------+
    | 1 | Joe | 85000 | 1 |
    | 2 | Henry | 80000 | 2 |
    | 3 | Sam | 60000 | 1 |
    | 4 | Max | 90000 | 1 |
    | 5 | Janet | 69000 | 1 |
    | 6 | Randy | 85000 | 1 |
    +----+-------+--------+--------------+

SELECT & FROM

select * from

select 可以選擇要印那一欄,如果加*可以把所有內如印出來,以下是指令:

select * from employee;

1
2
3
4
5
6
7
8
9
10
11
Table: employee;
+----+-------+--------+--------------+
| id | Name | Salary | DepartmentID |
+----+-------+--------+--------------+
| 1 | Joe | 85000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 1 |
| 4 | Max | 90000 | 1 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
+----+-------+--------+--------------+

select Name 指定欄位

SELECT Name, Salary From Employee;

Read more »

An unorder collection of a unquie object. No duplicate value.
不可以有重複的直,一般都是用在帳號上面。

Create set

1
2
my_set= {1,2,3,4,5}
print(my_set)

output:

{1, 2, 3, 4, 5}

如果加重複會用最前面的

1
2
my_set= {1,2,3,4,5,5}
print(my_set)

output:

{1, 2, 3, 4, 5}

adding set

1
2
3
4
5
my_set= {1,2,3,4,5,5}
my_set.add(100)
my_set.add(2)
print(my_set)

output

‘{1, 2, 3, 4, 5, 100}`

Read more »

今天想分享如何用 dictionary 字典方式存資料。

key and Value 如何使用宣槓取直

  • collection of of {key:value} pairs
  • no duplicate
  • allow changeable and ordered

declare dictionary 宣告 dict

It's an unorder , it's not next to each other, so we can't use index like we use in list. Dictionary will have a a key and value look like this:

syntax:

declare: dictname= {key:value}
get key: print(dictname['key'])

Access and Get value 取我們的直:

1
2
3
4
dict={
'keyA':1,
'keyB':2
}

取 key 的直: print(dict['keyA']) => 1
取所有直 print(dict) => {'keyA': 1, 'keyB': 2}

宣告不同 data type

Read more »

Tuple is like list, but we can;t modify it, they are immutable. Tuple immutable 直,就是他的直不無法修改的,有些語法是用const類似這概念。

Create Tuple 建立tuple

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Empty tuple
my_tuple = ()
print(my_tuple) #()

# Tuple having integers
my_tuple = (1, 2, 3)
print(my_tuple) #(1,2,3)

# tuple with mixed datatypes
my_tuple = (1, "Hello", 3.4)
print(my_tuple) #(1, "Hello", 3.4)

# multiply value
x,y,z, *other = (1,2,3,4,5)
print(f"x: {x}, y:{y}, z:{z}, other:{other}")
#x: 1, y:2, z:3, other:[4, 5]

my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple) #('mouse', [8, 4, 6], (1, 2, 3))

created without using parentheses

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a) # 3
print(b) # 4.6
print(c) # dog

my_tuple = ("hello")
print(type(my_tuple)) # <class 'str'>

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple)) # <class 'tuple'>

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple)) # <class 'tuple'>

Access Tuple Elements

1
2
3
4
5
6
7
8
9
10
11
12
# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0]) # 'p'
print(my_tuple[5]) # 't'

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3]) #'s'
print(n_tuple[1][1]) # 4

# Negative indexing for accessing tuple elements

1
2
3
4
5
6
7
8
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])
# elements beginning to 2nd
# Output: ('p', 'r')
print(my_tuple[:-7])

Slicing

Read more »

python 有很多不同 function,因此我想在這篇寫所有 funtion 我會用到。我指的 function 不是傳 function,而是一些 libary function

Math

Datetime 時間

time

程式跑的時間

如果計算程式袍多就可以用這方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import time

# starting time
start = time.time()
# your code
for i in range(1,60):
print(i)

#endtime
end = time.time()
runtime=end - start
print(" Runtime of the program is {runtime:%.8f}")
print(f"Runtime of the program is {runtime:.5f}")
#or
print(f"Runtime of the program is {(end - start):.5f}")

datetime

今天日期

  • date.today 取今天日期
Read more »

Pandas 筆記

今天我想分享我學的pandas筆記,我覺得對於分大數據很有幫助,他有點像mysql。請先下載module或package:pip install pandas`

Part1基本語法

連線CSV檔案

可以去github找相關csv檔案,然後去raw檔案做連線

1
2
3
4
5
import pandas as pd
url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv"
tips = pd.read_csv(url)

print(tips)

pandas會回傳DataFrame格式。<class 'pandas.core.frame.DataFrame'>
下面是印出所有資料表內容。

1
2
3
4
5
6
7
8
9
10
11
12
     total_bill   tip     sex smoker   day    time  size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
.. ... ... ... ... ... ... ...
239 29.03 5.92 Male No Sat Dinner 3
240 27.18 2.00 Female Yes Sat Dinner 2
241 22.67 2.00 Male Yes Sat Dinner 2
242 17.82 1.75 Male No Sat Dinner 2
243 18.78 3.00 Female No Thur Dinner 2

Head and Tail

  • head(N): 是查前面N筆資料

    print(tips.head(10))

    output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
       total_bill   tip     sex smoker  day    time  size
    0 16.99 1.01 Female No Sun Dinner 2
    1 10.34 1.66 Male No Sun Dinner 3
    2 21.01 3.50 Male No Sun Dinner 3
    3 23.68 3.31 Male No Sun Dinner 2
    4 24.59 3.61 Female No Sun Dinner 4
    5 25.29 4.71 Male No Sun Dinner 4
    6 8.77 2.00 Male No Sun Dinner 2
    7 26.88 3.12 Male No Sun Dinner 4
    8 15.04 1.96 Male No Sun Dinner 2
    9 14.78 3.23 Male No Sun Dinner 2
  • tail(N): 是查最後N筆資料

    print(tips.tail(10))

    output

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
      total_bill   tip     sex smoker   day    time  size
    234 15.53 3.00 Male Yes Sat Dinner 2
    235 10.07 1.25 Male No Sat Dinner 2
    236 12.60 1.00 Male Yes Sat Dinner 2
    237 32.83 1.17 Male Yes Sat Dinner 2
    238 35.83 4.67 Female No Sat Dinner 3
    239 29.03 5.92 Male No Sat Dinner 3
    240 27.18 2.00 Female Yes Sat Dinner 2
    241 22.67 2.00 Male Yes Sat Dinner 2
    242 17.82 1.75 Male No Sat Dinner 2
    243 18.78 3.00 Female No Thur Dinner 2

    Shape method

    shape 語法是用來查有多少欄與列

Read more »

Create empty Excel File

1
2
3
4
import openpyxl
fn = 'new_excel.xlsx'
wb = openpyxl.Workbook()
wb.save(fn)

Create Sheet工作表的

Create Empty Sheet 新增工作

1
2
3
4
5
6
import openpyxl
fn = 'new_excel.xlsx'
wb = openpyxl.Workbook()
wb.create_sheet("Sheet1", 0)
wb.create_sheet("Sheet2", 1)
wb.save(fn)

Read Excel Sheet 讀取excel檔案每個工作表的名稱

1
2
3
4
5
6
import openpyxl
fn = 'new_excel.xlsx'
wb = openpyxl.load_workbook(fn)
print(wb.sheetnames)
print(wb.active)
print(wb.active.title)

output:

1
2
3
['Sheet1', 'Sheet2', 'Sheet']
<Worksheet "Sheet1">
Sheet1

edit Name of worksheet 修改工作表名稱

Read more »