Fundamentals 5 min read

How to Parse Level‑Up Logs with Python and Pandas – Full Code Explained

This article walks through a Python interview problem that extracts level‑up events from a log file, demonstrating step‑by‑step data cleaning, JSON parsing, and aggregation with pandas, and provides the complete, ready‑to‑run code plus visual explanations.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Parse Level‑Up Logs with Python and Pandas – Full Code Explained

Introduction

Hello, I am PiPi. A few days ago a member of the Python Ultraman group asked a Python interview question. Below is the full code version of the solution, shared for fans who prefer a direct code snippet.

Implementation Process

Previously we posted an image version of the code, but the image was blurry and typing it manually was tedious. Here is the clean, runnable code:

import pandas as
import re
import json

file = "login.txt"
df1 = pd.read_table(file, engine="python", header=None)
df1.columns = ["col1"]
df2 = pd.DataFrame(columns=["日期", "id", "lv"])

k = 0
dic1 = {}
for i in df1.index:
    if "levelup" in df1.at[i, "col1"]:
        k += 1
        df2.at[k, "日期"] = df1.at[i, "col1"][1:11]
        pattern = r"{[^}]+}"
        match = re.search(pattern, df1.at[i, "col1"])
        if match:
            dic1 = json.loads(match.group())
            df2.at[k, "id"] = dic1["id"]
            df2.at[k, "lv"] = dic1["to_lv"]

df2.drop_duplicates(subset={"日期", "id"}, keep="last", inplace=True)
print(df2)
df2_grouped = df2.groupby(["日期", "lv"]).size().reset_index(name="Count")
print(df2_grouped)

The solution works by iterating over the log file, checking each line for the keyword "levelup", extracting the timestamp, parsing the embedded JSON for the user ID and target level, and then aggregating the data. Duplicate entries are removed, and the final DataFrame is grouped by date and level.

Conclusion

This article presents a Python interview programming problem, provides a detailed analysis, and shares the complete code implementation to help readers solve the task efficiently.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

pandaslog parsingCode Tutorialinterview question
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.