I have a text file containing 245K characters representing stocks' data in the following format :
{'SYMBOL_CODE': 'EGS923F1C016', 'Reuters': 'IEEC_r2.CA', 'HIGH_PRICE': 0.053, 'LOW_PRICE': 0.046, 'OPEN_PRICE': 0.054, 'CLOSE_PRICE': 0.051, 'LAST_TRADE_PRICE': 0.051, 'LAST_TRADE_VOLUME': 2050.0, 'TOTAL_TRADES': 499.0, 'TOTAL_VOLUME': 47991055.0, 'TOTAL_VALUE': 2366091.367000001, 'BID_PRICE': 0.051, 'BID_VOLUME': 27000.0, 'ASK_PRICE': 0.053, 'ASK_VOLUME': 215870.0, 'TOTAL_BIDS': 47039048.0, 'TOTAL_ASKS': 59442952.0, 'LAST_CHANGE': -0.003, 'LAST_CHANGE_PRC': -5.56, 'AVG_CHANGE': -0.003, 'AVG_CHANGE_PRC': -5.56, 'PREVIOUS_CLOSE': 0.054, 'HIGH_PRICE_LIMIT': 0.052, 'LOW_PRICE_LIMIT': 0.056, 'LOW_52_WEEk': 0.04, 'HIGH_52_WEEK': 0.121, 'TOTAL_BUY': 979337.838, 'TOTAL_SELL': 1386753.529, 'AVG_5Day': 41555071.0, 'AVG_30Day': 39937787.0, 'AVG_90Day': 39614361.0, 'LISTED_SHARES': 972000000, 'CURRENCY': 1, 'EPS': 0.0, 'PE_RATIO': 0.0, 'DIVIDEND_YIELD_PERC': 0.0, 'SYMBOL_STATE': 'A', 'SYMBOLS_REC_SERIAL': 627424338, 'TRADE_REC_SERIAL': 627424338}
Each stock's details are enclosed by {} as above, total of 314 stocks.
So i want to convert this txt file into a table using python to be as follows.
| SYMBOL_CODE | Reuters |
| ----------- | --------- |
| EGS923F1C016| IEEC_r2.CA|
and the rest of the details should follow the same structure.
The data shown looks like the string representation of a Python dictionary. Note: it is not JSON
You can interpret those data using ast.literal_eval
In order to construct a well-formatted output you will need to know the maximum width of all columns.
You could do it like this:
Output: