Skip to content

Instantly share code, notes, and snippets.

@hdknr
Created October 11, 2024 07:56
Show Gist options
  • Save hdknr/eb0e057830beda5efc3e5590c54e8e4f to your computer and use it in GitHub Desktop.
Save hdknr/eb0e057830beda5efc3e5590c54e8e4f to your computer and use it in GitHub Desktop.
ltsv

ltsv

ltsv から  csvに変換

#!/usr/bin/env python
import click

import pandas as pd

import csv
import re


@click.group()
@click.option("--tf_output", "-to", default=None)
@click.pass_context
def group(ctx, tf_output):
    pass


@group.command()
@click.argument("src")
@click.pass_context
def ltsv2csv(ctx, src):
    """LTSV to CSV"""

    output = f"{src}.csv"

    def get_line_dict(line):
        return dict(re.search(r"^([^\:]+)\:(.*)$", i).groups() for i in line)

    with open(src) as f:
        data = pd.DataFrame(map(get_line_dict, csv.reader(f, delimiter="\t")))
        data.to_csv(output, index=False)


if __name__ == "__main__":
    group()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment