Skip to content

Instantly share code, notes, and snippets.

@yuba
Created October 11, 2018 01:04
Show Gist options
  • Save yuba/aa220023f022ab2e72dd180ce6c92e78 to your computer and use it in GitHub Desktop.
Save yuba/aa220023f022ab2e72dd180ce6c92e78 to your computer and use it in GitHub Desktop.
LTSVをpandasに読み込む ref: https://qiita.com/yuba/items/0a9c8be19e32a00547e5
host:example.com url:/api/users status:200 time:2018-01-01T00:00:00+09:00
host:example.com url:/api/users status:200 time:2018-01-01T00:00:01+09:00
from collections import OrderedDict
import pandas as pd
def ltsv_to_data_frame(filename):
"""
LTSVファイルを読み込んでpandas DataFrameを生成します
値はすべて文字列型となります
"""
with open(filename) as f:
return pd.DataFrame(
[OrderedDict(cell.split(':', 1)
for cell
in line.rstrip('\r\n').split('\t'))
for line in f])
from io import StringIO # Python2の場合は from cStringIO
tsv = ltsv_to_data_frame(filename).to_csv(sep='\t', index=False)
df = pd.read_table(StringIO(tsv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment