Skip to content
The Chat Archive
minimax-m1 LMArena Human Preference 140K

how to read data in python colab? I have the data directory, and the data is in jsonl…

  1. User

    how to read data in python colab? I have the data directory, and the data is in jsonl format. show me the python code

  2. Assistant

    Here's a step-by-step solution to read JSONL data in Google Colab:

    # Step 1: Mount Google Drive
    from google.colab import drive
    drive.mount('/content/drive')
    
    # Step 2: Navigate to your data directory (adjust path as needed)
    %cd "/content/drive/MyDrive/your_data_directory"
    
    # Step 3: Read JSONL file
    import json
    
    def read_jsonl(file_path):
        data = []
        with open(file_path, 'r') as f:
            for line in f:
                data.append(json.loads(line))
        return data
    
    # Example usage
    file_path = "your_file.jsonl"  # Update with your filename
    jsonl_data = read_jsonl(file_path)
    
    # Display first 3 entries
    print("First 3 entries:")
    for i, entry in enumerate(jsonl_data[:3]):
        print(f"\nEntry {i+1}:")
        print(json.dumps(entry, indent=2))
    
    # Optional: Convert to pandas DataFrame
    import pandas as pd
    df = pd.DataFrame(jsonl_data)
    print("\nDataFrame shape:", df.shape)
    df.head()
    

    Key points:

    1. Mount Google Drive first to access your files
    2. Adjust the file path to match your directory structure
    3. The read_jsonl function handles large files efficiently
    4. Includes optional pandas conversion for data analysis

    Note: Make sure your JSONL file is properly formatted with one valid JSON object per line. You can check file existence using:

    !ls -l "your_file.jsonl"
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.