简体   繁体   English

如何使用 python glob 读取特定的 JSON 文件

[英]how to read specific JSON files using python glob

i have a set of JSON files in a folder.我在一个文件夹中有一组 JSON 个文件。

Sample files:示例文件:

-2022_06_13_07_14_f71cd512135bdab9.json
-2022_06_13_07_1421_f71cd512135bdab9.json
-2022_06_13_07_12314_f71cd512135bdab9.json
-2022_06_14_132_14_f71cd512135bdab9.json
-2022_06_14_74647_14_f71cd512135bdab9.json

Instead of reading all files at once, I need to read them day-wise.我不需要一次阅读所有文件,而是需要每天阅读它们。

ex:2022_06_13_07_14_f71cd512135bdab9.json corresponding to 2022_06_13.例如:2022_06_13_07_14_f71cd512135bdab9.json 对应 2022_06_13。

like wise I need to read all the JSON files and do the changes in Daywise batches.同样,我需要阅读所有 JSON 文件并在 Daywise 批次中进行更改。

read all JSON files in 2022_06_13 first then all the JSON files in 2022_06_14 and so on.首先读取 2022_06_13 中的所有 JSON 个文件,然后读取 2022_06_14 中的所有 JSON 个文件,依此类推。 i thought to apply wildcard with looping day wise list.我想在循环日明智列表中应用通配符。

my issue with the below line.我对以下行的问题。 How do I add a wild card to find all JSON files related to a particular date?如何添加通配符以查找与特定日期相关的所有 JSON 文件?

json_files = glob.glob(os.path.join(path_to_json, 'day*.json'))

current code:当前代码:

start = datetime.datetime(2022, 8, 25)
end = datetime.datetime(2022, 12, 25)
datelist = pd.date_range(start, end)
path_to_json = r'C:\Users\Admin\INPUT'

for a in datelist:
    day=str(a)[:10].replace('-', '_')
    json_files = glob.glob(os.path.join(path_to_json, 'day*.json'))
    
    for i in json_files:
        with open(i,'r') as fi:
            dict = json.load(fi)
            dict[0]["Name"] = "NETFLIX"
            fi.close()
        l= i.rsplit('\\', 1)[1]
        Output_URL="C:\\Users\\Admin\\OUTPUT\\Netflix\\"+l
        with open(Output_URL, "w+") as f:
            json.dump(data, f)

Change this: json_files = glob.glob(os.path.join(path_to_json, 'day*.json'))改变这个: json_files = glob.glob(os.path.join(path_to_json, 'day*.json'))

to this: json_files = glob.glob(os.path.join(path_to_json, f"{a.strftime('%Y_%m_%d')}*.json"))为此: json_files = glob.glob(os.path.join(path_to_json, f"{a.strftime('%Y_%m_%d')}*.json"))

This will take date a and turn it into a string that is similar to the one in your files.这将采用日期a并将其转换为类似于文件中的字符串。 Leave the asterisk after it as a wildcard and you should get all files that match date a everytime.将星号作为通配符保留在它后面,您应该每次都获得与日期匹配a所有文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM