I have this little problem. If i create a list from the content of a txt file and print it row by row there are huge spaces between these outputs.
Like:
Name
Name
Street
Thats how i want to style it:
Name
Name
Street
And this is the code:
import os.path
print('Get User Data')
print()
vName = input('Vorname: ')
nName = input('Nachname: ')
data = [vName, nName]
if os.path.isfile('data/'+vName+'_'+nName+'.txt'):
file = open('data/'+vName+'_'+nName+'.txt', 'r')
content = file.readlines()
for element in content:
print(element)
else:
data.append(input('Straße/Nr.:'))
file = open('data/'+vName+'_'+nName+'.txt', 'w')
for row in data:
file.write(row)
print()
print('New File created. --> /data/'+vName+'_'+nName+'.txt')
file.close()
Can someone explain why this happens and how to fix it? Thank you :)
You simply need to strip the new line character, \n
, before you print.
element.strip('\n')
will get the job done.
if os.path.isfile('data/'+vName+'_'+nName+'.txt'):
file = open('data/'+vName+'_'+nName+'.txt', 'r')
content = file.readlines()
for element in content:
element = element.strip('\n') # this is the line we add to strip the newline character
print(element)