dimthemainsjfdjlks
dimthemainsjfdjlks

Reputation: 64

Python - Printed Output Goes On Different Lines

When I'm trying to output my code, it goes on some random lines and the last line is fine. I'm a beginner. Just started 20 minutes ago.

f = open('gamelist.txt')

text = f.readlines()
for line in text:
    print('<div><a href="genericweb'+line+'.html">'+line+'</a></div>')

THIS IS MY OUTPUT

<div>
  <a
    href="genericwebabc
  .html"
    >abc
  </a>
</div>
<div>
  <a
    href="genericwebdef
  .html"
    >def
  </a>
</div>
<div><a href="genericwebghi.html">ghi</a></div>

As you can see, the first two don't work, but the last one does. The text document is: "abc def ghi". New lines in between each group of letters btw. Any help is appreciated

Upvotes: 1

Views: 543

Answers (2)

Hack5
Hack5

Reputation: 3601

You can set the end keyword-parameter to an empty string, and it will not print any ending (the newline). This parameter defaults to "\n", the escape for a newline. Setting it to be blank will cause this newline not to be printed. You can also set it to any other string to write a different end.

print("hello", end="")

You should probably take a look at Jinja2 for HTML templating in python.

Upvotes: 0

faiz-e
faiz-e

Reputation: 210

you may use :

print('<div><a href="genericweb'+line+'.html">'+line+'</a></div>', end=' ')

or try using this to get rid of newline :

for line in text:
    line = line.rstrip('\n')
    print('<div><a href="genericweb'+line+'.html">'+line+'</a></div>')

Hope it helps

Upvotes: 1

Related Questions