首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python读取JSON文件

使用Python读取JSON文件
EN

Stack Overflow用户
提问于 2021-10-16 10:55:06
回答 1查看 1.1K关注 0票数 0

我对python很陌生,我正在尝试读取一个JSON文件,目前我只需将其写入一个新文件,而不需要进行任何更改。我一直试图使用python包Click来完成这个操作,但始终会遇到错误。

我相信这是比较基本的,但任何帮助都将不胜感激。下面是我尝试过的代码的最新版本。

代码语言:javascript
复制
 import json
 import os
 import click


 def dazprops():
     """Read Daz3D property File"""
     path = click.prompt('Please specify a location for the Daz3D Properties File')
     path = os.path.realpath(path)
     #dir_name = os.path.dirname(path)
     print(path)
     dazpropsf = open('path')
     print(dazpropsf)


 if __name__ == '__main__':
     dazprops()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-16 12:14:32

像这样的东西可以让您知道如何使用click实现这一目标。

代码语言:javascript
复制
import click

def read_file(fin):
    content = None
    with open(fin, "r") as f_in:
        content = f_in.read()
    return content

def write_file(fout, content):
    try:
        print("Writing file...")
        with open(fout, "w") as f_out:
            f_out.write(content)
        print(f"File created: {fout}")
    except IOError as e:
        print(f"Couldn't write a file at {fout}. Error: {e}")

@click.command()
@click.argument('fin', type=click.Path(exists=True))
@click.argument('fout', type=click.Path())
def init(fin, fout):
    """
    FINT is an input filepath
    
    FOUT is an output filepath
    """
    content = read_file(fin)
    if content:
        write_file(fout, content)
                
if __name__ == "__main__":
    init()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69594900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档