【发布时间】:2020-04-11 01:29:15
【问题描述】:
我正在寻找有关 glob 和 pandas to_csv 的路径制作的解决方案,有人有解决方案吗?
我的代码:
from glob import glob
import json
import pandas as pd
PathIn = 'c:\\Users\\***\\PycharmProjects\\Project\\In'
PathOut = 'c:\\Users\\***\\PycharmProjects\\Project\\Out'
for fileName in glob(PathIn + '*.json', recursive=True):
with open(fileName, 'rb') as f:
json_dict = json.load(f)
print(json_dict)
.
.
.
.
.
.
df.to_csv(PathOut + fileName + '.csv', sep=";")
他不打印我的 JSON 文件,所以不要在我的 In.而且我的输出中没有任何 CSV。
【问题讨论】:
-
检查你的缩进,如果你想使用 f 那么它需要在你的 with 语句中缩进,否则文件将被关闭。您当前形式的代码在语法上不正确。
-
@ChrisDoyle 很抱歉,这只是我创建帖子的时候,但所有代码都在声明中
-
你试过简单化你的问题吗?只需打印文件,看看您是否阅读了您所期望的内容。例如
for fileName in glob(PathIn + '*.json', recursive=True):与写glob('c:\\Users\\***\\PycharmProjects\\Project\\In' + ''*.json''相同,与glob('c:\\Users\\***\\PycharmProjects\\Project\\In*.json''相同 -
我认为你的问题出在你写目录的方式上,我建议使用
os.path.join(PathIn, '*.json')然后写os.path.join(PathOut, filename),否则你会错过一个``。 -
尝试在输入和输出目录的末尾添加一个`\`
标签: python json pandas csv glob