点击登录

工具 csv/json互转换脚本(避免修改index.csv时误操作)

手表具体型号
通用

2minRain

二分之雨
⚡认证创作者
🔥创作者
社区会员
米坛社区百万注册纪念勋章
本脚本无需安装任何第三方库
主要功能就是CSV转JSON/JSON转CSV

AI太好用了

命令:
修改前执行:
py ./convert.py index.csv index.json
然后直接修改json文件, 随后运行:
py ./convert.py index.json index.csv
会直接覆盖原有的csv文件, 随后commit, push即可

提供exe版本和脚本内容

脚本内容如下:
Python:
import csv
import json
import argparse

def csv_to_json(csv_file, json_file):
    # 读取CSV文件并转换为JSON格式
    with open(csv_file, mode='r', encoding='utf-8') as file:
        reader = csv.DictReader(file)
        data = [row for row in reader]
    with open(json_file, mode='w', encoding='utf-8') as file:
        json.dump(data, file, indent=4, ensure_ascii=False)

def json_to_csv(json_file, csv_file):
    # 读取JSON文件并转换为CSV格式
    with open(json_file, mode='r', encoding='utf-8') as file:
        data = json.load(file)
    with open(csv_file, mode='w', encoding='utf-8', newline='') as file:
        writer = csv.DictWriter(file, fieldnames=data[0].keys())
        writer.writeheader()
        writer.writerows(data)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="CSV和JSON互转换")
    parser.add_argument("input_file", help="输入文件路径")
    parser.add_argument("output_file", help="输出文件路径")
    parser.add_argument("--to-json", action="store_true", help="将CSV转换为JSON")
    parser.add_argument("--to-csv", action="store_true", help="将JSON转换为CSV")

    args = parser.parse_args()

    if args.to_json:
        csv_to_json(args.input_file, args.output_file)
    elif args.to_csv:
        json_to_csv(args.input_file, args.output_file)
    else:
        print("请指定转换方向:--to-json 或 --to-csv")
 

附件

本脚本无需安装任何第三方库
主要功能就是CSV转JSON/JSON转CSV

AI太好用了

命令:
修改前执行:
py ./convert.py index.csv index.json
然后直接修改json文件, 随后运行:
py ./convert.py index.json index.csv
会直接覆盖原有的csv文件, 随后commit, push即可

提供exe版本和脚本内容

脚本内容如下:
Python:
import csv
import json
import argparse

def csv_to_json(csv_file, json_file):
    # 读取CSV文件并转换为JSON格式
    with open(csv_file, mode='r', encoding='utf-8') as file:
        reader = csv.DictReader(file)
        data = [row for row in reader]
    with open(json_file, mode='w', encoding='utf-8') as file:
        json.dump(data, file, indent=4, ensure_ascii=False)

def json_to_csv(json_file, csv_file):
    # 读取JSON文件并转换为CSV格式
    with open(json_file, mode='r', encoding='utf-8') as file:
        data = json.load(file)
    with open(csv_file, mode='w', encoding='utf-8', newline='') as file:
        writer = csv.DictWriter(file, fieldnames=data[0].keys())
        writer.writeheader()
        writer.writerows(data)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="CSV和JSON互转换")
    parser.add_argument("input_file", help="输入文件路径")
    parser.add_argument("output_file", help="输出文件路径")
    parser.add_argument("--to-json", action="store_true", help="将CSV转换为JSON")
    parser.add_argument("--to-csv", action="store_true", help="将JSON转换为CSV")

    args = parser.parse_args()

    if args.to_json:
        csv_to_json(args.input_file, args.output_file)
    elif args.to_csv:
        json_to_csv(args.input_file, args.output_file)
    else:
        print("请指定转换方向:--to-json 或 --to-csv")
 

*这是一则由 Google AdSense 自动推荐的广告,与本站无关,不对其真实性与可靠性负责

Users who are viewing this thread

Home 首页
Home 资源
News 发现
Account 我的
顶部