Last active
February 11, 2019 04:06
-
-
Save Nasdin/d5682e675a040a051065d7418487c94d to your computer and use it in GitHub Desktop.
Fastest String to CSV parser
This file contains hidden or 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
| import time | |
| import pandas as pd | |
| def string_to_csv(string: str, csv_output_name: str): | |
| row_gen = (x.split("=") for x in string.split(';')) | |
| df = pd.DataFrame(row_gen) | |
| df['index'] = df.groupby(0).cumcount() | |
| df = df.pivot(index='index', values=1, columns=0) | |
| df.to_csv(csv_output_name) | |
| return df | |
| start = time.time() | |
| test_string = "TTMNPMGN=31.28954;NLOW=307.97567;TTMPRCFPS=27.16872;TTMGROSMGN=49.10444;TTMCFSHR=13.00898;QCURRATIO=1.20643;TTMREV=301399.3;TTMINVTURN=486.1752;TTMOPMGN=38.86435;TTMPR2REV=11.19504;AEPSNORM=7.57146;TTMNIPEREM=2105243;EPSCHNGYR=60.0117;TTMPRFCFPS=29.49287;TTMRECTURN=15.61345;TTMPTMGN=38.10252;QCSHPS=17.31687;TTMFCF=114406.6;LATESTADATE=2017-12-31;APTMGNPCT=37.10254;AEBTNORM=89233.92;TTMNIAC=92496.23;NetDebt_I=16365.67;PRYTDPCTR=-8.26516;TTMEBITD=118195.1;AFEEPSNTM=8.9858;PR2TANBK=12.3999;EPSTRENDGR=40.56326;QTOTD2EQ=56.58907;TTMFCFSHR=11.98382;QBVPS=33.64562;NPRICE=345;YLD5YAVG=0.25561;REVTRENDGR=40.19977;TTMEPSXCLX=9.68724;QTANBVPS=28.64635;PRICE2BK=10.25394;MKTCAP=3374178;TTMPAYRAT=0;TTMINTCOV=-99999.99;TTMDIVSHR=0.88;TTMREVCHG=54.42107;TTMROAPCT=15.60043;TTMROEPCT=34.0467;TTMREVPERE=6728263;APENORM=45.56584;TTMROIPCT=22.18601;REVCHNGYR=48.38553;CURRENCY=CNY;DIVGRPCT=56.26752;TTMEPSCHG=72.78667;PEEXCLXOR=35.61385;QQUICKRATI=1.2046;TTMREVPS=31.57088;BETA=1.10179;TTMEBT=114840.7;ADIV5YAVG=0.47269;ANIACNORM=72201.45;QLTD2EQ=49.80237;NHIG=476.6" | |
| out_df = string_to_csv(test_string, "data.csv") | |
| end = time.time() | |
| print(out_df) | |
| print("Total Duration : {}".format(end - start)) | |
| """ Total Duration: 0.012""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment