按照 CELLID 排序並寫入新檔案 data = sorted(data, key=lambda x: int(float(x['CELLID'])))…
Jump to turn (6)
- 1按照 CELLID 排序並寫入新檔案 data = sorted(data, key=lambda x: int(float(x['CELLID']))) la
- 2with open(file\name, 'w', newline='') as file: writer = csv.writer(file) writer.
- 3按照 CELLID 排序並寫入新檔案,並添加新行 ... with open(file\name, 'a', newline='') as file: writ
- 4繼續說
- 5加入新資料列 writer.writerow(['', datetime.now().strftime("%H:%M"), '']) print(f'Saved
- 6def main(): while True: download\and\save\data() time.sleep(120) # 等待 2 分鐘再次檢查 這
- User
按照 CELLID 排序並寫入新檔案
data = sorted(data, key=lambda x: int(float(x['CELLID']))) last_parking_status = {row['CELLID']: row['ParkingStatus'] for row in data} file_name = f'{datetime.now().strftime("%Y-%m-%d %H-%M-%S")}.csv'
這是什麼意思
- Assistant
這段程式碼是Python的程式碼,主要目的是排序一個包含多個字典的列表(List of dictionaries)
data,並將排序後的結果寫入一個新的CSV檔案中。具體來說,程式碼的功能可以分為以下幾個步驟:- 使用Python的
sorted函數,按照字典中'CELLID'對應的值進行排序,其中lambda x: int(float(x['CELLID']))是一個排序的key function,表示將每個字典中的'CELLID'值轉換成浮點數再轉換成整數進行排序。排序後的結果將存回原本的data列表中。 - 創建一個新的字典
last_parking_status,用於存儲最後一次每個'CELLID'對應的'ParkingStatus'的值。具體來說,程式碼中的{row['CELLID']: row['ParkingStatus'] for row in data}表示遍歷data列表中的每個字典,將其對應的值作為字典中的key,對應的值作為value,最終得到一個到的映射字典。
- 使用Python的