Kangar
Kangar

Reputation: 9

Python Read Textfile Formatting

I have a textfile containing the following information:

* A05 A12 A15 A19 A23
B023 1 0 -1 -- 1
B221 1 1 0 -1 --
B260 0 1 1 -1 -1
B231 0 0 1 1 --
B456 1 1 -1 1 -1

What i'm trying to do is to print and format the data inside this textfile to something like this:

*    |A05 |A12 |A15 |A19 |A23
-----|----|----|----|----|---
B023 |1   |0   |-1  |--  | 1
B221 |1   |1   | 0  |-1  |--
B260 |0   |1   | 1  |-1  |-1
B231 |0   |0   | 1  | 1  |--
B456 |1   |1   |-1  | 1  |-1
-----|----|----|----|----|---

How might i go about doing this?

Upvotes: 1

Views: 67

Answers (3)

nikeros
nikeros

Reputation: 3379

I have written the code with the idea of leaving some options open regarding formatting:

def init_formatters(n_cols, key_col_len, val_col_len):
    line_format = "{:<P}|".replace("P", str(key_col_len)) + (n_cols-1) * "{:>P}|".replace("P", str(val_col_len))
    divisor_format = "{:-<P}|".replace("P", str(key_col_len)) + (n_cols-1) * "{:-<P}|".replace("P", str(val_col_len))
    return line_format, divisor_format

def format_lines(lines, key_col_len = 10, val_col_len=5):
    initialized = False
    for line in lines:
        tokens = re.split("\s+", line)
        if not initialized:
            line_format, divisor_format = init_formatters(len(tokens), key_col_len, val_col_len)
        print(line_format.format(*tokens))
        if not initialized:
            initialized = True
            print(divisor_format.format(*(len(tokens)*["-"])))
    print(divisor_format.format(*(len(tokens)*["-"]))) 

format_lines can be called passing the lines you want to format: you can choose a different width for the key column vs the data columns:

l = ["* A05 A12 A15 A19 A23 A24 A25 A26",
"B023 1 0 -1 -- 1 2 3 1",
"B221 1 1 0 -1 -- 1 1 1",
"B260 0 1 1 -1 -1 0 3 2",
"B231 0 0 1 1 -- 6 3 2",
"B456 1 1 -1 1 -1 1 2 4"]


format_lines(l, 5, 4) # this will format the table as desired

Upvotes: 0

Anurag Kumar Singh
Anurag Kumar Singh

Reputation: 31

You can do this in the following way. Here I am assuming the number of columns is fixed.

Just pass the content of the file in the format_table_elements function. It prints the formatted table. You can modify it to return the table as a string.

string = """
* A05 A12 A15 A19 A23
B023 1 0 -1 -- 1
B221 1 1 0 -1 --
B260 0 1 1 -1 -1
B231 0 0 1 1 --
B456 1 1 -1 1 -1
"""


def parse_table_elements(string):
    result = []
    senteces = string.split("\n")
    for i in senteces:
        result.append(i.split(" "))
    return result

def format_table_elements(string):
    string = string.strip()
    table_elemets = parse_table_elements(string)
    for i in table_elemets:
        print ("{:<5}|{:<4}|{:<4}|{:<4}|{:<4}|{:<3}".format(i[0], i[1], i[2], i[3], i[4], i[5]))
        if table_elemets.index(i) == 0:
            print("-----|----|----|----|----|---")

    print("-----|----|----|----|----|---")

format_table_elements(string)

OUTPUT

*    |A05 |A12 |A15 |A19 |A23
-----|----|----|----|----|---
B023 |1   |0   |-1  |--  |1  
B221 |1   |1   |0   |-1  |-- 
B260 |0   |1   |1   |-1  |-1 
B231 |0   |0   |1   |1   |-- 
B456 |1   |1   |-1  |1   |-1 
-----|----|----|----|----|---

Upvotes: 1

Sash Sinha
Sash Sinha

Reputation: 22360

Say you have a file called data.txt with the following contents:

* A05 A12 A15 A19 A23
B023 1 0 -1 -- 1
B221 1 1 0 -1 --
B260 0 1 1 -1 -1
B231 0 0 1 1 --
B456 1 1 -1 1 -1

You could consider utilizing the prettytable package:

import prettytable

with open('data.txt', 'r') as data_file:
    table = prettytable.from_csv(data_file)
    
print(table)

Output:

+------+-----+-----+-----+-----+-----+
|  *   | A05 | A12 | A15 | A19 | A23 |
+------+-----+-----+-----+-----+-----+
| B023 |  1  |  0  |  -1 |  -- |  1  |
| B221 |  1  |  1  |  0  |  -1 |  -- |
| B260 |  0  |  1  |  1  |  -1 |  -1 |
| B231 |  0  |  0  |  1  |  1  |  -- |
| B456 |  1  |  1  |  -1 |  1  |  -1 |
+------+-----+-----+-----+-----+-----+

Upvotes: 1

Related Questions