Stephen Roda
Stephen Roda

Reputation: 899

How do I format text from a file in Python?

I have a file that has 50 lines (each line has a name on it) and I would like to take each line and read it, then print it, but in a format that has 5 columns and 10 lines per column.

It would like something like this:

xxxxx -- xxxxx -- xxxxx -- xxxxx -- xxxxx

xxxxx -- xxxxx -- xxxxx -- xxxxx -- xxxxx

xxxxx -- xxxxx -- xxxxx -- xxxxx -- xxxxx

xxxxx -- xxxxx -- xxxxx -- xxxxx -- xxxxx

xxxxx -- xxxxx -- xxxxx -- xxxxx -- xxxxx

This is the code I have so far. It just reads and prints each line on a newline:

f = open('file.txt', 'r')
for x in f:
    x = x.lstrip()
    x = x.rstrip()
    x = x.title()
    print x

The reason for the x.lstrip and x.rstip and x.title is because the text is formatted all weird in the file so I have to do that. This is for an assignment. Thanks!

Upvotes: 0

Views: 21110

Answers (4)

jfs
jfs

Reputation: 414149

The following is not suitable for a beginner. Consider it just one of many options (and not the best way) to do it.

You can read 5 lines at a time using the answer for What is the most “pythonic” way to iterate over a list in chunks?:

#!/usr/bin/env python3
import fileinput

# read 5 lines at a time from stdin or file(s) at the command-line
for row in zip(*[iter(fileinput.input())]*5):
    print(' -- '.join("%-4s" % item.strip() for item in row))

Note: it expects exactly 5*m lines in the input to produce m output rows.

fileinput.input() returns an iterator over input lines and here's an explanation how the above code group the lines.

Example

$ python reformat-input.py input.txt
a0   -- b0   -- c00  -- d0   -- e0  
a1   -- b111 -- c1   -- d1   -- e1  
a2   -- b2   -- c2   -- d2   -- e2 

Where input.txt is:

a0
b0
c00
d0
e0
a1
b111
c1
d1
e1
a2
b2
c2
d2
e2

Upvotes: 0

Dan Gerhardsson
Dan Gerhardsson

Reputation: 1889

Something like this might work:

def print_table(lines, col_num, col_width):
    for line_ix in range(0, len(lines), col_num):
        print ' -- '.join([line.strip().ljust(col_width) for line in lines[line_ix:line_ix+col_num]])

Upvotes: 1

Mike Corcoran
Mike Corcoran

Reputation: 14565

does this do what you want? (i might have interpreted what you needed incorrectly) you want 5 lines from the file displayed per row of the new output, each line seperated by ' -- '?

f = open('file.txt', 'r')
text = ""
lines = []
counter = 0

for line in f:
    line = line.strip()
    if counter % 5 == 0 and text > '':
        counter = 0
        lines.append(text)
        text = ''

    text += line + ' '
    counter += 1  


for line in lines:
    vals = tuple(line.split())
    print('%s -- %s -- %s -- %s -- %s' % (vals))

Upvotes: 0

KevinShaffer
KevinShaffer

Reputation: 858

you could try to use the file type .dat, and then f = loadtxt("file.dat") and that will put your data into an array, using the columns and rows you already have created in the file. You would have to edit the data file a bit, but it certainly would work. and if you wanted to change things even more you could use commands like this,

c = f[:,5] which will create a new array of 1 column, with all the data from column 5 from your original file

Upvotes: 0

Related Questions