How to split text into separate lines?

Question:

I have this text and i need to split it into separate lines:

text = """
| Post code | Cost, thousands USD  |
|-----------+----------------------|
| 33022     |                0.543 |
| 33145     |             9563.214 |
| 33658     |               85.543 |
| 33854     |                0.010 |
| 33698     |          1000000.000 |
"""

expecting output with 7 separate lines or list with seven elements that include a single line

Asked By: gleb

||

Answers:

My approach would be to split this string by "n" and to reference only the lines where are the values and split them by "|", removing additional spaces to add each item in a tuple.

text = """
| Post code | Cost, thousands USD  |
|-----------+----------------------|
| 33022     |                0.543 |
| 33145     |             9563.214 |
| 33658     |               85.543 |
| 33854     |                0.010 |
| 33698     |          1000000.000 |
"""

records = []

for i in text.strip().split("n")[2:]:
    row = i[3:-2].split("|")
    values = tuple(j.strip() for j in row)
    records.append(values)

With this, records will be equals to a list of tuples where each value is a column:

[('3022', '0.543'),
 ('3145', '9563.214'),
 ('3658', '85.543'),
 ('3854', '0.010'),
 ('3698', '1000000.000')]

If you want an one-line solution:

records = [tuple(j.strip() for j in i[3:-2].split("|")) for i in text.strip().split("n")[2:]]

If you want to keep the headers:

headers = [tuple(i.strip() for i in text.strip().split("n")[0][1:-1].split("|"))]
records = headers + records
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.