【问题标题】:How do i print a json output into a specific file path?如何将 json 输出打印到特定的文件路径中?
【发布时间】:2017-10-29 10:16:20
【问题描述】:

我在将输出保存到特定文件路径(如 csv 文件输出)时遇到问题,只是我的代码更简单,而且它具有“with open(xxx)”。

这是我的代码。如何将“输出”打印到特定文件路径中,例如 /home/pi/Desktop/sample.json?

    import csv
    import json

    with open(csvfile, "a") as output:
       fieldnames = ['Name', 'Number', 'ID', 'Address']
       writer = csv.writer(output,delimiter = ',', lineterminator="\n")
       writer.writerows([fieldnames])

【问题讨论】:

标签: json python-3.x csv converter


【解决方案1】:

最简单的选择是让您的 Python 代码保持原样并使用 shell 的强大功能!

假设脚本在您当前的工作目录中,试试这个:

python myscript.py > sample.json

这会将脚本的输出 (stdout) 重定向到自动创建它的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2011-12-19
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 2011-05-05
    相关资源
    最近更新 更多