Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8071345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:40:34+00:00 2026-06-05T13:40:34+00:00

I have a lot of archive data in python dict format that I am

  • 0

I have a lot of archive data in python dict format that I am working to convert over to json. I’m interested in speeding this process up if I can and would like to know if anyone has any suggestions. Currently I:

  1. take in the gzip compressed data “in multiple files”
  2. read them line by line
  3. do a literal_eval with ast
  4. use json dumps to to create the needed json string
  5. query for the date of the line
  6. open up a file for appending named by the detected date
  7. write the string to the file
  8. restart the entire process

Here’s my current working code:

import gzip
import ast
import json
import glob
import fileinput
from dateutil import parser

line = []

filestobeanalyzed = glob.glob('./data/*.json.gz')

for fileName in filestobeanalyzed:
        inputfilename = fileName
        print inputfilename # to keep track of where I'm at
        for line in fileinput.input(inputfilename, openhook=fileinput.hook_compressed):
                line = line.strip();
                if not line: continue
                try:
                        line = ast.literal_eval(line)
                        line = json.dumps(line)
                except:
                        continue
                date = json.loads(line).get('created_at')
                    if not date: continue
                date_converted = parser.parse(date).strftime('%Y%m%d')
                outputfilename = gzip.open(date_converted, "a")
                outputfilename.write(line)
                outputfilename.write("\n")

outputfilename.close()

There must be a more efficient way of doing this, I’m just not seeing it. Does anyone have any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:40:36+00:00Added an answer on June 5, 2026 at 1:40 pm

    First profile it with http://packages.python.org/line_profiler/ it will give you timings line by line, you can also try to multithreaded it, use multiprocessing pool and map a list of files to your function.

    Though I have feeling IO maybe an issue, though again that depends on the size of the files.

    # Assuming you have a large dictionary across a multi-part gzip files.
    def files_to_be_analyzed(files):
        lines = ast.literal_eval("".join([gzip.open(file).read() for file in files]))
        date = lines['created_at']
        date_converted = parser.parse(date).strftime('%Y%m%d')
        output_file = gzip.open(date_converted, "a")
        output_file.write(lines + "\n")
        output_file.close()
    

    Im still not quite sure what you are trying to achieve, but try to read and write as few times as possible since IO is a killer. If you need to work on multiple directories then look at multithreading it something like this.

    import gzip
    import ast
    import json
    import glob
    import fileinput
    from dateutil import parser
    from multiprocessing import Pool
    
    # Assuming you have a large dictionary across a multi-part gzip files.
    def files_to_be_analyzed(files):
        lines = ast.literal_eval("".join([gzip.open(file).read() for file in files]))
        date = lines['created_at']
        date_converted = parser.parse(date).strftime('%Y%m%d')
        output_file = gzip.open(date_converted, "a")
        output_file.write(lines + "\n")
        output_file.close()
    
    if __name__ == '__main__':
        pool = Pool(processes = 5) # Or what ever number of cores you have
        directories = ['/path/to/this/dire', '/path/to/another/dir']
        pool.map(files_to_be_analyzed, [glob.glob(path) for path in directories])
        pools.close()
        pools.join()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read a lot about soft deletes and archive and saw all the
We have lot of object with this kind of design : Interface and several
I am creating Excel file download using NOPI libraries. I have lot of data
I am trying to build a data mart. I have lot of dimensions, and
I have a lot of html files that I want to add a rel=nofollow
I currently have this huge and slow code that takes forever to run.... Im
I'm writing a game that will have a lot of information (configuration, some content,
There is a lot of (older) related data spread over many tables in our
We have a process that is getting data in real time and adding records
I have some image-files stored into one file (some kind of archive). That file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.