Transfer TXT file to a table in Python

66 views Asked by At

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.

1

There are 1 answers

0
SIGHUP On

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:

from ast import literal_eval
from itertools import repeat

FILE = "foo.txt"

def do_print(lst, width):
    print("|" + "|".join(f"{k:^{width+2}}" for k in lst) + "|")

with open(FILE) as f:
    if dicts := [literal_eval(r) for r in f]:
        width = 0
        for d in dicts:
            for k, v in d.items():
                width = max(width, len(k), len(str(v)))
        do_print(dicts[0].keys(), width)
        do_print(repeat("-" * width, len(dicts[0])), width)
        for d in dicts:
            do_print(d.values(), width)

Output:

|     SYMBOL_CODE     |       Reuters       |     HIGH_PRICE      |      LOW_PRICE      |     OPEN_PRICE      |     CLOSE_PRICE     |  LAST_TRADE_PRICE   |  LAST_TRADE_VOLUME  |    TOTAL_TRADES     |    TOTAL_VOLUME     |     TOTAL_VALUE     |      BID_PRICE      |     BID_VOLUME      |      ASK_PRICE      |     ASK_VOLUME      |     TOTAL_BIDS      |     TOTAL_ASKS      |     LAST_CHANGE     |   LAST_CHANGE_PRC   |     AVG_CHANGE      |   AVG_CHANGE_PRC    |   PREVIOUS_CLOSE    |  HIGH_PRICE_LIMIT   |   LOW_PRICE_LIMIT   |     LOW_52_WEEk     |    HIGH_52_WEEK     |      TOTAL_BUY      |     TOTAL_SELL      |      AVG_5Day       |      AVG_30Day      |      AVG_90Day      |    LISTED_SHARES    |      CURRENCY       |         EPS         |      PE_RATIO       | DIVIDEND_YIELD_PERC |    SYMBOL_STATE     | SYMBOLS_REC_SERIAL  |  TRADE_REC_SERIAL   |
| ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
|    EGS923F1C016     |     IEEC_r2.CA      |        0.053        |        0.046        |        0.054        |        0.051        |        0.051        |       2050.0        |        499.0        |     47991055.0      |  2366091.367000001  |        0.051        |       27000.0       |        0.053        |      215870.0       |     47039048.0      |     59442952.0      |       -0.003        |        -5.56        |       -0.003        |        -5.56        |        0.054        |        0.052        |        0.056        |        0.04         |        0.121        |     979337.838      |     1386753.529     |     41555071.0      |     39937787.0      |     39614361.0      |      972000000      |          1          |         0.0         |         0.0         |         0.0         |          A          |      627424338      |      627424338      |