在 csv file 中寫入中文資料

前言 原先要為了不同作業系統處理不同編碼,但原本 UTF-8 就應該可以被所有作業系統給讀取,於是研究了一下並把注意事項做成筆記! UTF-8 vs UTF-8-sig 兩個都是 UTF-8 的編碼,不過 UTF-8-sig(UTF-8 with signature) 是在檔案開頭帶了 \ufeff 的BOM(Byte Order Mark),定義一個,讓打開他的檔案知道這是個 UTF-8 編碼的檔案。 開啟方式的解決方案 在開啟檔案的時候,直接透過記事本或是 VSCode 其實是可以讀取到正確的內容的,但是在 Excel 中打開會是亂碼,那是因為 Excel 沒有讀到 BOM(Byte Order Mark),所以沒辦法正確的解碼中文字,那麼就會顯示亂碼了。 使用記事本或是 VSCode 就會用正確的編碼開啟了。 寫入方式的解決方案 剛剛提到前面少了 BOM 讓 Excel 不知道怎麼開,那就在開頭加上 BOM,要在檔案中寫入 BOM 的方法有兩種,一個是使用 UTF-8-sig,另一個就是自己寫 BOM 進去檔案了,這樣就可以讓編輯器知道要用 UTF-8 來解碼這個檔案! 使用 UTF-8-sig with open(path, 'w', newline='', encoding='utf-8-sig') as csv_file: csv_file.write(data) 寫入 BOM with open(path, 'w', newline='', encoding='utf-8') as csv_file: # 寫入 UTF-8 BOM csv_file.write(u'\ufeff'.encode('utf8')) csv_file.write(data) 注意事項 寫入方式的解決方案都是在檔案開頭帶了一個 BOM,讓編輯器看得懂檔案,不過在下載的檔案需要被匯入系統的場景就需要特別注意,因為對於程式來說 BOM 就是多了一個字元在前面,匯入時就需要處理掉那個字元,不然就會導致讀取錯誤! ...

2024-11-18 · 84 words · SekiXu

Flask 生命週期 - Flask life cycle

前言 完全不懂 Flask life cycle 我們也可以使用 Flask 建立出一個完整的系統,但是瞭解了之後,我們可以將各種資源的調度放在 Flask life cycle 的各個節點中,這樣可以讓整個系統更有條理,以下內容皆整理自 Flask 官方文件,我也會寫一個範例 Repo 幫助讀者更有效地理解 Flask life cycle。 https://flask.palletsprojects.com/en/3.0.x/lifecycle/ Flask handle request with WSGI server 以下步驟節錄自 Python 官方文件 Browser or other client makes HTTP request. WSGI server receives request. WSGI server converts HTTP data to WSGI environ dict. WSGI server calls WSGI application with the environ. Flask, the WSGI application, does all its internal processing to route the request to a view function, handle errors, etc. Flask translates View function return into WSGI response data, passes it to WSGI server. WSGI server creates and send an HTTP response. Client receives the HTTP response. 簡單說明: WSGI server 負責接收來自 Client 端的請求(step 1, 2),再將請求轉成 Python 可以執行的內容(step 3~5),讓 Python 處理完之後的內容打包 http response (step 6, 7)回應請求端(step8)。 ...

2024-05-21 · 241 words · SekiXu