我是一个Python新手,但在Google+GPT的帮助下,我正在尝试在我的M1 Mac(12.4)上运行一个脚本,但什么都没有发生。
我不知道这是否重要,但我正在跑蟒蛇。当我在终端中键入“which python3”时,我会得到以下响应:/Library/framework/Python.framework/Versions/311/bin/python3
我键入“python3”,然后粘贴.py文件(/Users/myusername/Documents/file.py)的位置,然后按回车键,但什么都没有发生。
顶行是我键入“python3+.py文件的路径名”的位置
如果作业在我的Documents文件夹中的所有.json文件中的“job_type”为AA或BB,python脚本应该从每个“job”的“end”字段中减去600(而不是“depot”)。
以下是.py文件的内容:
import os import json # Path to the folder containing the json files folder_path = '/Users/myusername/Documents/' # Loop through all the files in the folder for filename in os.listdir(folder_path): # Check if the file is a json file if filename.endswith('.json'): # Open the file and read its contents with open(os.path.join(folder_path, filename), 'r') as json_file: data = json.load(json_file) # Loop through the jobs array for job in data['jobs']: # Check if the job type is "AA" or "BB" if job['job_type'] in ["AA", "BB"]: # Subtract 600 from the end field job['end'] -= 600 # Write the modified data back to the file with open(os.path.join(folder_path, filename), 'w') as json_file: json.dump(data, json_file, indent=4)
这里是文件夹中一个.json文件的示例。
{ "zone": { "zone_id": D, "end_time": 60000 }, "depots": [ { "depot_id": 4, "lat": -87.600738, "lng": 41.788975, "start": -3600, "end": 10096, "label": "Label1", "packing_time": 600, "depot_type": "AA" }, { "depot_id": 3, "lat": -84.390186, "lng": 33.749178, "start": -3600, "end": 10696, "label": "Label1", "packing_time": 600, "depot_type": "BB" } ], "jobs": [ { "job_id": 1, "depot_id": 4, "lat": 37.802334, "lng": -122.417907, "status": "ready", "label": "A1A2", "order_placed": 1800, "start": -960, "end": 5340, "pickup_eta": null, "dropoff_eta": null, "volume": null, "products": [], "job_type": "AA", "service": 240 }, { "job_id": 31, "depot_id": 3, "lat": 37.802334, "lng": -122.417907, "status": "ready", "label": "B1B2", "order_placed": 1800, "start": -960, "end": 5340, "pickup_eta": null, "dropoff_eta": null, "volume": null, "products": [], "job_type": "BB", "service": 240 } { "job_id": 31, "depot_id": 3, "lat": 37.802334, "lng": -122.417907, "status": "ready", "label": "C1C2", "order_placed": 1800, "start": -960, "end": 5340, "pickup_eta": null, "dropoff_eta": null, "volume": null, "products": [], "job_type": "CC", "service": 240 } ], "drivers": [ { "driver_id": 1, "lat": 37.4767822, "lng": -122.261175, "status": "ready", "time_until_completion": null, "label": "Name1", "assigned_jobs": [], "depot_id": null, "capacity": null }, { "driver_id": 1, "lat": 37.4767822, "lng": -122.261175, "status": "ready", "time_until_completion": null, "label": "Name2", "assigned_jobs": [], "depot_id": null, "capacity": null } ] }