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 6144391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:38:35+00:00 2026-05-23T18:38:35+00:00

I have some JSON formatted log files that I am copying to S3 so

  • 0

I have some JSON formatted log files that I am copying to S3 so I can run Hive queries on them using Elastic Map Reduce. The script I use to copy the log files to S3 is written in Python.

Every once in a while I encounter a file with an incomplete line, typically at the end of the file. This causes any Hive queries that need that file to fail. I’ve been manually fixing the files by removing the bad line, but I’d like to integrate this step into my Python script to prevent these failures.

Here’s an example of the type of file I’m working with:

{"logLine":{"browserName":"FireFox","userAgent":"Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0"}}
{"logLine":{"browserName":"Pre","userAgent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.24 (KHTML, like Gecko; Google Web Preview) Chrome/11.0.696 Safari/534.24"}}
{"logLine":{"browserName":"Internet Explorer","userAgent":"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1

In that case I want to remove the last line since it’s incomplete. I know it’s incomplete because it’s missing the end of line character(s), and also because it’s not valid JSON due to the missing end quote and curly braces.

Is there an easy way to identify and remove that file from the file using Python?

  • 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-05-23T18:38:36+00:00Added an answer on May 23, 2026 at 6:38 pm

    Python has a json module in its standard library. It has a parser that will raise an exception if the input isn’t valid JSON. To check the last line, you could do something like

    import json
    with open('log.txt') as file:
        lines = file.readlines()
    try:
        json.loads(lines[-1])
    except ValueError:
        with open('log.txt', 'w') as file:
            file.write(''.join(lines[:-1]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data that I have to serialize to JSON. I'm using JSON.NET.
I have some json formatted data that I parse with JSON.parse. The problem I
I have an ajax request that returns some JSON formatted data. I'm building out
I have some json files with 500MB. If I use the "trivial" json.load() to
I have an jQuery JSON request, that loads some JSON from another server (ex.
I have a spring action that I am rendering some json from the controller,
Hi, i have been having some problems using JSON data in flash builder lately
I have a text file that is formatted like JSON, but in a print/view
I have an application that is sending a JSON object (formatted with Prototype) to
I have some experience using JSON to pull data from a .asp page. How

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.