# BWNC i 供養者資料整理 運行腳本教學 ## 環境 - [BWNC i 網站](https://bwnc.ecwid.com/) - [BWNC i 後台](https://my.ecwid.com/store/14018368#orders:offset=300&count=100) - 登入後,在 My Sales :point_right: Orders - Python3 terminal(小黑窗) ## 資料整理 ### Step1. 登入時使用 Google 登入 ### Step2. 從後台 Download 要整理的資料 在最底部選擇 Export All ![](https://i.imgur.com/DW3Znlk.png) Delimiter 選擇 `Comma`,再選擇以下這些欄位即可,選擇` Download CSV file` ![](https://i.imgur.com/k5TyuiX.png) ### Step3. 在本機端整理資料 > 首先有個叫 Script 的資料夾,其中要有幾個檔案: > - DownloadFileName.csv (剛剛download 下來的檔案) > - script.py > - modify_data.py > > ![](https://i.imgur.com/60L1Fp1.png) 1. 打開 Terminal,移動到Script 資料夾的位置 - ex. Mac: `cd /Users/monicashiao/Desktop/Script` - ex. Window: 2. 打開 script.py,將第6行`download_filename` 後的字串換成**下載檔案的名字**,並存檔。 ![](https://i.imgur.com/xtKYdW8.png) 3. 執行 script.py(即是已經處理好的資料) - ex. Mac: `python3 script.py` - ex. Window: ![](https://i.imgur.com/F48qxni.png) 4. output_DownloadFileName.csv 產生 ![](https://i.imgur.com/GSewGql.png) 5. 檢查`中文姓名`欄位後的所有欄位與options欄位內容比較,看看有無缺失(因為可能options 中的名稱不同導致抓不到資料),若抓不到請到modify_data.py 中做**名稱字串**的添加 ![](https://i.imgur.com/Dmj0M6h.png) ### 程式碼 #### script.py ```=py # -*- coding: utf-8 import csv from modifyData import * download_filename = "orders_2020-07-01_00-00" with open("./" + download_filename + ".csv", 'r') as read_obj, \ open("./output1_" + download_filename + ".csv", 'w', newline='') as write_obj: # Create a DictReader object from the input file object dict_reader = csv.DictReader(read_obj) # Get a list of column names from the csv field_names = dict_reader.fieldnames # Add new column names which will be processed by old data field_names += ["中文姓名", "英文姓名", "Email", "班級(廣論學員請填)","迴向名1", "迴向名1_email", "迴向名2", "迴向名2_email", "迴向名3", "迴向名3_email", "迴向名4", "迴向名4_email", "收據人姓名(英文)", "收據人email", '迴向名(總)'] # Create a DictWriter object from the output file object by passing column / field names dict_writer = csv.DictWriter(write_obj, field_names) # Write the column names in output csv file dict_writer.writeheader() # Read each row of the input csv file as dictionary for row in dict_reader: #if row["sku"] == "00023" or row["sku"] =="00022" or row["sku"] == "00083": # Modify the dictionary / row by passing it to the transform function (the callback) transformRow(row) # Write the updated dictionary or row to the output file dict_writer.writerow(row) print("Successful!") ``` #### modify_data.py ```=py # -*- coding: utf-8 customer_name = ['供養大德姓名 (中文,與後面付款人一致):','功德主芳名(中文):', '祈福者姓名(中文):', '所迴向的人的名字(中文):','供養大德姓名 (中文):', '祈福者姓名:', '供養大德姓名:','贊助者姓名(中文):', '功德主芳名(中文):','供養大德姓名(中文):','供養大德姓名(中文,與後面付款人一致)'] customer_english_name = ['your English name:', 'Your English Name:', 'your English name (英文):', 'English Name:', '有人English name (英文):', 'your English name'] customer_email = ['Your Email:', 'your email:', 'Email:', 'your email'] customer_class = ['廣論班級(廣論學員請填):', '消費者班級(Class):', '收據人班級:', '廣論班級 (廣論學員請填):', '廣論班級 (廣論學員請填)我要迴向給(限一個姓名,每個名字最多六個字)', '我要迴向給(限一個姓名,每個名字最多六個字)'] transference1_name = ['我要迴向給(限一個姓名,每個名字最多六個字):', '我要迴向給(限一個名字,每個名字最多六個字):', '我要迴向給(限一個姓名,每個名字最多六個字):', '我要迴向給(限一個姓名):', '我要迴向給(限一個姓名):', '供養對象:', '我要迴向給1(限一個姓名):', '被迴向名字1(限一個姓名):','我要迴向給(限一個姓名,最多六個字):','我要迴向給(限一個姓名,最多6個字):','被迴向名字1 (限一個姓名,最多六個字):','我要迴向給(限四個姓名,每個名字最多6個字):','我要迴向給的在世者(限一個姓名,最多六個字):','我要迴向給:','我要迴向給(限一個姓名,最多6個字):', '我要迴向給(限四個姓名,每個名字最多6個字)', '我要迴向給(限六個姓名,每個名字最多6個字)', '我要迴向給(限一個姓名,每個名字最多6個字):'] transference1_email = ['迴向對象的Email:', '迴向對象 的email:', '迴向對像1 的email:', '被迴向1的Email:'] transference2_name = ['被迴向名字2(限一個姓名):', '我要迴向給2(限一個姓名):','被迴向名字2 (限一個姓名,最多六個字):','我要迴向給的往生者(限一個姓名,最多六個字):'] transference2_email = ['被迴向2的Email:', '迴向對像2 的email:'] transference3_name = ['被迴向名字3(限一個姓名):','被迴向名字3 (限一個姓名,最多六個字):','我要迴向給3(限一個姓名):'] transference3_email = ['被迴向3的Email:'] transference4_name = ['被迴向名字4(限一個姓名):','被迴向名字4 (限一個姓名,最多六個字):','我要迴向給4(限一個姓名):'] transference4_email = ['被迴向4的Email:'] receipt_name = ['收據人(Receipt Receiver):', '收據人姓名 (英文):', '收據人姓名(英文):', '收據人姓名:'] receipt_email = ['收據人email:'] options_name = ['中文姓名', '英文姓名', 'Email', '班級(廣論學員請填)', '迴向名1', '迴向名1_email', '迴向名2', '迴向名2_email', '迴向名3', '迴向名3_email', '迴向名4', '迴向名4_email', '收據人姓名(英文)', '收據人email', '迴向名(總)'] list_name = [customer_name, customer_english_name, customer_email, customer_class, transference1_name, transference1_email, transference2_name, transference2_email, transference3_name, transference3_email, transference4_name, transference4_email, receipt_name, receipt_email] def getContent(string, search_list, num): for ele in search_list: if ele in string: start = string.find(ele) + len(ele) end = string.find('\n', start) end = len(string) if end == -1 else end return "" if start >= end else string[start:end].replace(':', '') def transformRow(row): row_options = row['options'] transference_name = '' for num in range(0,14): row[options_name[num]] = getContent(row_options, list_name[num], row['order_number']) if any(ele in row_options for ele in list_name[num]) else "" # collect all transference names in one column for n in range(4,11,2): transference_name += row[options_name[n]] + "\n" if row[options_name[n]] != '' else '' row[options_name[14]] = transference_name ```