2020年5月22日 星期五

python 函數 def

def multiply(m, n):
    return m * n

print(multiply(2, 3))

python 字串處理

#####################################
s="0123456789"
print(s[:5])
print(s[:5:2])
s.find("2") #  找字

#####################################
# 印出三角*
for i in range(0,10):
    for j in range(0,i+1):
        print("*",end="") # 不換行
    print("")

#####################################
#Longest Common SubSequence
import numpy as np

S1="abcdefg"
S2="abzzcdzzefg"
L1 = list(S1)
L2 = list(S2)
#補零作為起始
L1.insert(0,"0")
L2.insert(0,"0")
A = np.zeros((len(L1), len(L2)), dtype=np.int)
#初始值
for i in range(0,len(L1)):
    A[i,0] = -i
for j in range(0,len(L2)):
    A[0,j] = -j
#計算距離矩陣
for i in range(1,len(L1)):
    for j in range(1,len(L2)):
        if L1[i] == L2[j]:
            A[i,j] = A[i-1,j-1] + 2
        else:
            A[i,j] = max(A[i-1,j-1],A[i,j-1],A[i-1,j]) - 1

#回溯字串
i = len(L1)-1
j = len(L2)-1
LCS1 = ""
LCS2 = ""
while i > 0 and j > 0:
    #左上
    if A[i-1,j-1] >= max(A[i,j-1],A[i-1,j]):
        LCS1 = L1[i] + LCS1
        LCS2 = L2[j] + LCS2
        i = i - 1
        j = j - 1
    #左
    elif A[i,j-1] >= A[i-1,j]:
        LCS1 = "-" + LCS1
        LCS2 = L2[j] + LCS2
        j = j - 1
    #上
    else:
        LCS1 = L1[i] + LCS1
        LCS2 = "-" + LCS2
        i = i - 1

while i > 0:
    LCS1 = L1[i] + LCS1
    LCS2 = "-" + LCS2
    i = i - 1

print("輸入的字串")
print(S1)
print(S2)
print("輸出的字串")
print(LCS1)
print(LCS2)
print("共同的字串")
for i in range(0,len(LCS1)):
    if LCS1[i] == LCS2[i]:
        print(LCS1[i],end="")

2020年5月19日 星期二

python MySQL

import pyodbc
import pandas as pd

cn = pyodbc.connect('DRIVER={MySQL ODBC 8.0 ANSI Driver};SERVER=localhost;DATABASE=my_db;USER=root;PASSWORD=xxxx;OPTION=3;')
SQL = "select * from expense"
df = pd.read_sql(SQL, cn)
cn.close()

2020年5月13日 星期三

python 判斷是否為數字

def is_number(str):
    try:
        # 因為使用float有一個例外是'NaN'
        if str=='NaN':
            return False
        float(str)
        return True
    except ValueError:
        return False

print(is_number("123"))



str為字串s為字串
str.isalnum() 所有字元都是數字或者字母
str.isalpha() 所有字元都是字母
str.isdigit() 所有字元都是數字
str.isspace() 所有字元都是空白字元、t、n、r

2020年5月12日 星期二

python 日期 週的使用

import datetime
from_date = (datetime.date.today()-datetime.timedelta(days=1)).strftime('%Y-%m-%d')
#週的第一天
#python星期定義為一到日:0~6
#日期寫法:datetime.datetime(2020, 5, 11)
today = datetime.date.today()
#週的第幾天(以0起算)
week_days = (today.weekday()+1) % 7
week_start = today - datetime.timedelta(days=week_days)
print(week_start)

delta = datetime.timedelta(days=7)
MFG_DAY = week_start - delta * 10

for i in range(0,11):
    print((MFG_DAY + delta * i).strftime('%Y/%m/%d'),"第",(MFG_DAY + delta * i).isocalendar()[1],"週")

2020年5月10日 星期日

python 檔案處理

#複製檔案
import shutil
source = "//10.88.39.45/mfg/AI_Picture/weilunwu/test1/aaa.txt"
target = "//10.88.39.45/mfg/AI_Picture/weilunwu/test2/aaa.txt"
shutil.copyfile(source,target)

2020年4月24日 星期五

ORA-12154: TNS: 無法解析指定的連線 ID

最近朋友使用 EF 連到 Oracle ,在本機上執行的好好的。
將程式部署到另一台機器就發生了 「ORA-12154: TNS: 無法解析指定的連線 ID」的錯誤,如下,
環境變數也都有加入 TNS_ADMIN 呀!
後來只好在 web.config 中再加入 TNS_ADMIN 的設定,如下,
<configuration>
  <oracle.manageddataaccess.client>
    <version number="*">
      <settings>
        <setting name="TNS_ADMIN" value="C:\oracle\network\admin**請調整機器上的Path "/>
      </settings>
    </version>
  </oracle.manageddataaccess.client>
</configuration>