【问题标题】:How to set path with glob when the fileName is use for csv?当文件名用于 csv 时,如何使用 glob 设置路径?
【发布时间】: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


【解决方案1】:

这里的关键是您想根据输入文件在相关用户目录中创建输出文件,因此您可以只获取用户目录列表并迭代每个用户目录然后设置输入和输出文件搜索 json 文件并在对应的目录中创建 csv。类似的东西。

import json
from glob import glob
import os.path as op
basepath = r'C:\Users\***\PycharmProjects'
_in = 'In'
_out = 'Out'
suffix = '\*.json'
output_suffix = '.csv'

for path in glob(basepath):
    in_dir = op.join(path, _in)
    out_dir = op.join(path, _out)
    for json_file in glob(in_dir + suffix, recursive=True):
        in_file_name = op.basename(json_file)
        out_file_name = in_file_name.split('.')[0] + output_suffix
        output_file = op.join(out_dir, out_file_name)
        with open(json_file) as jf:
            json_data = json.load(jf)
        print(json_data)

        ###do some stuff with the json

        with open(output_file, 'w') as of:
            of.write("some data or json stuff")

【讨论】:

  • 是的,但是在使用 pandas 时如何获取文件名,因为我使用数据框来配置我的 CSV
  • 什么意思?在您的帖子中,您从路径打开文件。为什么现在你说我怎么取文件名?
  • df.to_csv(PathOut + fileName + '.csv', sep=";") 我使用文件名来创建我的 CSV,在您的示例中,您创建没有 pandas 数据框的 csv,它在我使用 pandas 的代码中编写
  • 所以只需执行df.to_csv(output_file, sep=";"),因为我们已经创建了输出文件路径/名称
  • 好的,我知道我会试试这个
【解决方案2】:

只是稍微修改您的代码我认为您在输入目录中编写搜索路径时错过了\。 对于输出目录,您需要通过将扩展名.json 替换为.csv 来构建文件名。有很多方法可以做到这一点:

for fileName in glob(PathIn + '\*.json', recursive=True):        
    with open(fileName, 'rb') as f:
        json_dict = json.load(f)
        print(json_dict)

    out_file_name = os.path.split(fileName)[0] + '.csv'
    out_file_dir = os.path.join(PathOut, out_file_name)
    # Here do something with your output file

【讨论】:

  • 但是 outdir 只是一个字符串,它不会被写入用户目录,outfile 将如何知道 in 文件来自哪个用户目录并知道如何将其写入匹配的用户 out 文件目录?
  • 从 OP 代码中我看到文件和输出目录之间没有联系,代码只是检查输入目录中的 .json 文件,对其进行处理并保存相应的 csv输出目录中的文件。输入和输出文件之间的唯一联系是它们具有相同的名称但不同的扩展名。
  • PathOut = 'c:\\Users\\***\\PycharmProjects\\Project\\Out' 你认为路径中的* 是什么意思呢?
猜你喜欢
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-08
  • 1970-01-01
  • 2020-06-30
  • 1970-01-01
  • 2018-08-28
相关资源
最近更新 更多