【发布时间】:2022-01-19 08:48:15
【问题描述】:
我正在使用 python 并且我编写了脚本,它从 .csv 生成一个 .json 文件......它工作得很好,但我想将这些 json 文件保存到不同的文件夹中。
我在哪里写路径?
import csv
import json
import glob
import os
for filename in glob.glob("Csv/*.csv"):
csvfile = os.path.splitext(filename)[0]
jsonfile = csvfile + '.json'
with open(csvfile+'.csv') as f:
reader = csv.DictReader(f)
rows = list(reader)
with open(jsonfile, 'w') as f:
json.dump(rows, f,indent=4)
【问题讨论】:
-
open()的文档说file is a path-like object giving the pathname (absolute or relative to the current working directory)。也就是说,您可以在对open的调用中指定要保存文件的绝对(或相对)路径。 docs.python.org/3/library/functions.html#open