使用 Django 中的 Pandas 或 CSV/Json 库将 Json 输出转换为 CSV 创建 CSV 文件

问题描述 投票:0回答:1

我正在努力弄清楚如何获取已经制作的 Json 输出,并从该输出创建一个 csv 文件。为了获得更多说明,我使用 write 创建了一个 json 文件,但我不确定如何在同一过程中创建 CSV 文件。也许我只是没有在正确的区域输入 CSV 代码,或者 Django 需要某种其他形式的方法来处理该请求。

utils.py

import json
import os
import pandas as pd

from django.utils import timezone
from django.http import HttpResponse
from django.db.models import Q
from api.models import BlockedList

def get_ip_list_json() -> dict:

    json_response = {
        "version": "",
        "description": "",
        "objects": [
            {
                "name": "",
                "id": "",
                "description": "",
                "ranges": ["2.2.2.2"],
            },
        ],
    }

    group_map = {}

    for ip in BlockedList.objects.filter(
        Q(end_date__isnull=True) | Q(end_date__gte=timezone.now())
    ).select_related("group"):
        group_id = str(ip.group.id)
        if group_id not in group_map:
            group_map[group_id] = {
                "name": ip.group.name,
                "id": ip.group.group_id,
                "description": ip.group.description,
                "ranges": [],
            }
        group_map[group_id]["ranges"].append(ip.address)

    json_response["objects"] = list(group_map.values())
    return json_response

def create_ip_file():
    try:
        base_dir = "C://Users/Steven/Desktop"
        filename = "blocklist.json"

        full_path = os.path.join(base_dir, filename)

        with open(full_path, "w") as f:
            f.write(json.dumps(get_ip_list_json(), indent=4))
    except Exception as e:
        print(e)

这是我创建 Json 输出文件的上面的代码,下面是我尝试过的代码,但不确定如何将其适合当前进程,或者我是否必须以不同的方向进行处理.

with open("blocklist.json", enconding="utf-8") as inputfile:
     df = pd.read_json(inputfile)

df.to_csv("test.csv", encoding="utf-8", index=False)
data = get_ip_list_json()

json_data = json.loads(data)
df = pd.DataFrame(json_data)

csv_file = "test.csv"
df.to_csv(csv_file, index=False)
with open("blocktest.json") as inputfile:
   Data = json.loads(get_ip_list_json())

with open("test.csv", "w") as outfile:
   f = csv.writer(outfile)
   f.writerows(Data["Objects"])

我尝试过实现这三种方法,但似乎没有生成 csv 文件,我不知道为什么。当使用该 util 文件时,他们似乎也没有创建错误,因此正在读取代码但没有生成文件。

python python-3.x django
1个回答
0
投票

简单。

生成 JSON 数据 -> 将 JSON 数据写入文件 -> 将 JSON 数据转换为 Pandas DataFrame -> 将 DataFrame 写入 CSV 文件。

import json
import os
import pandas as pd

from django.utils import timezone
from django.http import HttpResponse
from django.db.models import Q
from api.models import BlockedList

def get_ip_list_json() -> dict:

    json_response = {
        "version": "",
        "description": "",
        "objects": [
            {
                "name": "",
                "id": "",
                "description": "",
                "ranges": ["2.2.2.2"],
            },
        ],
    }

    group_map = {}

    for ip in BlockedList.objects.filter(
        Q(end_date__isnull=True) | Q(end_date__gte=timezone.now())
    ).select_related("group"):
        group_id = str(ip.group.id)
        if group_id not in group_map:
            group_map[group_id] = {
                "name": ip.group.name,
                "id": ip.group.group_id,
                "description": ip.group.description,
                "ranges": [],
            }
        group_map[group_id]["ranges"].append(ip.address)

    json_response["objects"] = list(group_map.values())
    return json_response

def create_ip_file():
    try:
        base_dir = "C://Users/Steven/Desktop"
        json_filename = "blocklist.json"
        csv_filename = "blocklist.csv"

        # Create JSON file
        json_full_path = os.path.join(base_dir, json_filename)
        with open(json_full_path, "w") as f:
            json_data = get_ip_list_json()
            f.write(json.dumps(json_data, indent=4))
        
        # Convert JSON data to DataFrame
        objects = json_data["objects"]
        data_for_csv = []
        for obj in objects:
            for ip in obj["ranges"]:
                data_for_csv.append({
                    "name": obj["name"],
                    "id": obj["id"],
                    "description": obj["description"],
                    "ip_range": ip
                })

        df = pd.DataFrame(data_for_csv)

        # Create CSV file
        csv_full_path = os.path.join(base_dir, csv_filename)
        df.to_csv(csv_full_path, index=False)
    
    except Exception as e:
        print(e)
© www.soinside.com 2019 - 2024. All rights reserved.