一、How to concate file path - os.path.join
import os.path
print( os.path.join(os.path.sep, "D:", "TEMP", "a.txt") )
#result => \D:TEMP\a.txt #一般不該把根目錄放在 JOIN,會造成分隔字元在最前面
mypath = os.path.join(os.path.sep, 'TEMP', 'a.txt')
#字頭有 os.path.sep 與沒有會影響決對路徑的位置
#目前有 \ 所以會捉到目前 Python 所在磁碟的根目錄 + \TEMP\a.txt
print(os.path.abspath(mypath))
#result => D:\TEMP\a.txt
mypath = ''.join(["C:", mypath])
print(mypath)
#result => C:\TEMP\a.txt
os.path.sep : 目前 os 預設的磁碟分隔符 (Windows : "\", Lundix,Unix : "/")
os.path.abspath : 回傳決對路徑,當傳入的是 os.path.abspath("\TEMP") => 目前程式的錄徑磁碟 + \TEMP => D:\TEMP,但如果是 os.path.abspath("TEMP") => 目前 Python 程式的位置 + \TEMP
二、Create a file and insert string to file
f = open("D:\data2.txt", "w") #W : open file for read/write
f.write( "TEST" )
f.close()
三、Get Current Work Folder - os.getcwd()
目前檔案放在 D:\TEMP 目錄下,並寫入下面的 Code
import os
path = os.getcwd()
print(path)
#RESULT : D:\TEMP
四、Convert BASE64biary To JPG File (Python 2.7 only)
img_data = b'/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAC0ALQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWG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0Tg08GoAaeDVpmbRODTgahDU4NVpmbRMGpc1EGpQ1O5NiWio91Lup3FYfRTN1G6i4WH0UzdRuouFh9JmmbqQtRcdhxamk00tTS1S2NIcTTCaQmmE1LZaQE1GTSk0wmobNEgzRTM0VNy7AKcDRRSQ2PBpwooq0ZscDTgaKKoljgaXNFFMkXNGaKKYhc0ZoopiDNJmiigYmaM0UUgGk0hNFFIpDSaYaKKlloaaYaKKhlobRRRUlH/2Q=='
# In Python 2.7
fh = open("d:\imageToSave.png", "wb") #wb : open file for read/write by binary mode
fh.write(img_data.decode('base64'))
fh.close()
# In Python 3.X
import base64
imgdata = base64.b64decode(img_data)
filename = 'c:/temp/test.png'
with open(filename, 'wb') as f:
f.write(imgdata)
五、How to use REGULAR EXPRESSION in python
如何使用 regular expression 捉取字串中的數字,並依捉取的順序群組的方式取得
import re
#reObj = re.compile(r'(\d\d).*(\d\d)')
reObj = re.compile(r'([0-9]+).* ([0-9]+)')
mo = reObj.search('my age 13 and weight 65')
print(mo.group(1))
print(mo.group(2))
print(mo.group())
print(mo.groups())
print(len(mo.groups()))
print(mo.span()) #取得目前符合字串的 index
mo1, mo2 = mo.groups() #使用兩個變數直接取回 mo 的 groups 內的值
print(mo1)
print(mo2)
>>> 13
>>> 65
>>> 13 and weight 65
>>> ('13', '65')
>>> 2
>>> (7, 23)
>>> 13
>>> 65
上面的做法是截取字串回來,另一種作法是取得符合條件的座標
import re
pattern = "[0-9]+.*[0-9]+"
string = 'my age 13 and weight 65'
result = re.search(pattern,string).span()
print(result) #取得座標 (加 span 才會轉換成座標)
result = re.compile(pattern).findall(string)
print(result) #截取回字串
>>> (7, 23)
>>> ['13 and weight 65']
六、How to use python open/write file
下面的例子主要是透過 Open 的方式,產生 Input 與 Output 的檔案,
import os
openFile = open("d:\\input.txt", "r")
writeFile = open("d:\\output.txt", "w")
txtLine = openFile.readlines()
for text in txtLine:
#text = text.replace("\n", "") #取代掉換行符號
print(text)
writeFile.write(text)
writeFile.close()
openFile.close()
七、How to install package in Visual Studio C#
在 Visual Studio 中,選擇 Python Environments,按下右鍵會有 Install Python Package,安裝後即可在該 Python 2.7 目錄下看到安裝的套件,如果要指定版本,就下 openpyxl==2.1.4 即可
在網站 http://pypi.org/project/ 找尋 pip,進入 https://pip.pypa.io/en/stable/installing/ 下載 get-pip.py 進行安裝,並且使用 python get-pip.py 進行安裝
D:\>python
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
>>> import openpyxl
或
>>> python -c "import openpyxl"
反安裝 : python -m pip uninstall openpyxl
更新 pip : python -m pip install --upgrade pip
1. pinrt_func.py
def print_name(a):
print(a)
2. main_program.py
from print_data import print_name
print_name('abc')
or
import print_data
print_data.print_name('abc')
透過上面的寫法,from <python file> import <function name or * (不建議)> 即可把匯入自己撰寫的 py 檔內 def 供其他程式使用
1) PYTHONPATH : 將要匯入的 py 檔所在的路徑加入環境變數
2) sys.path.append("模組化文件目錄")
import sys
print(sys.path)
如果出現的結果沒有你 py 檔指定的目錄,則可以透過下面方式加入
import sys
sys.path.append("d:\my_python_lib")
3) PYTHON 安裝目錄/lib : 將檔案直接放到 python 安裝目錄的 lib 下
沒有留言:
張貼留言