简体   繁体   中英

Loop through multiple json input files

I have over 200 scrapped files in json format and i want to analyse then. i can open them individually, but would like to loop through to save time as i will be doing this a lot.

can open each file but want to be able to do a loop in some format eg

with codecs.open('c:\\project\\input*.json','r','utf-8') as f:

where '*' is a number.....

import codecs, json, csv, re

#read a json file downloaded with twitterscraper

with codecs.open('c:\\project\\input1.json','r','utf-8') as f:
    tweets = json.load(f,encoding='utf-b')

Just put your files into a folder and then loop through the files in the folder like so.

import codecs
import json
import csv
import re
import os

files = []
for file in os.listdir("/mydir"):
    if file.endswith(".json"):
        files.append(os.path.join("/mydir", file))

for file in files:
    with codecs.open(file,'r','utf-8') as f: 
        tweets = json.load(f,encoding='utf-b')

Add, and use, glob to iterate over files with certain file pattern.

import glob
import codecs
import json
# ... more packages here

for file in glob.glob('c:\\project\\input*.json'):
    with codecs.open(file, 'r','utf-8') as f: 
        tweets = json.load(f, encoding='utf-b')
        #... whatever you do next with `tweets`

BTW: utf-b instead of utf-8?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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