import json#更新的文件名path = root1232022-08-10_1jsonwith openpath r as f row_data = jsonloadf #将文件内容按照decimal_start_ip进行排序 row_datasortkey=lambda x intxdecimal_start_ip reverse=False#转换指定格式 start
The script will have the following problems when processing the data:
-
The data is not in the correct format. The script assumes that the input data is a JSON array with each element being a dictionary. However, the given data is not in this format. It is a list of dictionaries. To fix this, the data should be wrapped in square brackets to form a JSON array.
-
The script sorts the data based on the "decimal_start_ip" key. However, the given data does not have a "decimal_start_ip" key. It has a "start_ip" key instead. To fix this, the key used for sorting should be changed to "start_ip".
-
The script writes the transformed data to a file named "1.json" using the "ab+" mode. However, it is not clear if this is the intended behavior or if the file should have the same name as the input file. To fix this, the file name and the mode should be adjusted according to the desired behavior.
Here is the modified script with the above issues addressed:
import json
# Input data
data = [
{"city_cd": 501, "city_en_name": "Wuhan", "city_name": "武汉市", "continent_cd": 1, "continent_en_name": "Asia", "continent_name": "亚洲", "country_cd": 148, "country_en_name": "China", "country_name": "中国", "create_time": "2023-09-14 14:21:22", "decimal_end_ip": 143503481, "decimal_start_ip": 143503481, "end_ip": "8.141.176.121", "latitude": 30.4673, "longitude": 114.401, "province_cd": 42, "province_en_name": "Hubei", "province_name": "湖北省", "start_ip": "8.141.176.121", "update_time": "2023-09-14 14:21:22"},
{"city_cd": 12121, "city_en_name": "Taipei", "city_name": "台北市", "continent_cd": 1, "continent_en_name": "Asia", "continent_name": "亚洲", "country_cd": 148, "country_en_name": "China", "country_name": "中国", "create_time": "2023-09-14 13:15:46", "decimal_end_ip": 3659726960, "decimal_start_ip": 3659726944, "end_ip": "218.35.0.112", "latitude": 25.0376, "longitude": 121.564, "province_cd": 12, "province_en_name": "Taiwan", "province_name": "台湾省", "start_ip": "218.35.0.96", "update_time": "2023-09-14 13:15:46"},
{"city_cd": 12126, "city_en_name": "Changhua", "city_name": "彰化县", "continent_cd": 1, "continent_en_name": "Asia", "continent_name": "亚洲", "country_cd": 148, "country_en_name": "China", "country_name": "中国", "create_time": "2023-09-14 13:15:53", "decimal_end_ip": 1914327103, "decimal_start_ip": 1914327103, "end_ip": "114.26.80.63", "latitude": 24.0756, "longitude": 120.544, "province_cd": 12, "province_en_name": "Taiwan", "province_name": "台湾省", "start_ip": "114.26.80.63", "update_time": "2023-09-14 13:15:53"}
]
# Sort the data based on the "start_ip" key
data.sort(key=lambda x: (x['start_ip']))
# Convert the data to the specified format
formatted_data = []
for entry in data:
formatted_entry = "{}|{}|{}|0|{}|{}|0\n".format(
entry["start_ip"], entry["end_ip"], entry["country_en_name"], entry["province_en_name"], entry["city_en_name"]
)
formatted_data.append(formatted_entry)
# Write the formatted data to a file
output_path = "/root/123/2022-08-10_1_transformed.json"
with open(output_path, "w") as f:
f.writelines(formatted_data)
In the modified script, the input data is updated to match the given data. The sorting key is changed to "start_ip". The formatted data is stored in a list and then written to a file named "2022-08-10_1_transformed.json".
原文地址: https://www.cveoy.top/t/topic/jcai 著作权归作者所有。请勿转载和采集!