-4

So i have this code in python:

payment = open ("paintingJobs.txt" , "r")
lines = payment.readlines()
print (lines), ["A"]

[Jobs][1]

And, i'm trying to print only the lines of document that contain an "A" in them in a suitable format (not scruffy). I've tried the code above but it just prints out the whole document in an unorganised way.

Would there be a way to present the printed info like this: Table

2 Answers2

0

print (lines), ["A"] is not the correct syntax for printing every element of lines that contains the letter A. Instead, try:

payment = open ("test.py" , "r")
lines = payment.readlines()
for line in lines:
    if "A" in line:
        print(line)
payment.close()

Or, alternatively,

payment = open ("test.py" , "r")
lines = payment.readlines()
print([line for line in lines if "A" in line])
payment.close()
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • Thank You so much and also i would like to ask you if it is possible to present the printed info like this: http://prntscr.com/9wizsd – Computer_119911 Jan 29 '16 at 20:16
  • I don't see why not. You can use the third party module [Pillow](https://python-pillow.github.io/) to render text onto an image. There isn't going to be a simple one line `render_data_beautifully(payment)` function though, if that's what you're asking. – Kevin Jan 29 '16 at 20:18
  • I mean is there a way to implement .format for the information so it looks like a table, so each bit of info is separated by an equal space hence giving it the appearance of a table. – Computer_119911 Jan 29 '16 at 20:23
  • You can get nicely spaced columns, yeah. Check out [Python format tabular output](http://stackoverflow.com/q/8356501/953482). You'll probably need to get your data into a two-dimensional list though. – Kevin Jan 29 '16 at 20:31
0

You have use with

with open("test.py" , "r") as payment
    lines = payment.readlines()
    print([line for line in lines if "A" in line])