Created
October 11, 2018 01:04
-
-
Save yuba/aa220023f022ab2e72dd180ce6c92e78 to your computer and use it in GitHub Desktop.
LTSVをpandasに読み込む ref: https://qiita.com/yuba/items/0a9c8be19e32a00547e5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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