As shown by the print out, each line should consist of aa name that has had its characters reversed followed by a birthdate that has had its format converted to a European format.
The last line however prints out two of these lines next to each other without a space. I was wondering how to avoid this and have both names/birthdates print on separate lines as those above them.
def mirror(text):
back = ""
for i in range(1, len(text)):
back = back + text[len(text) - i]
return back
while True:
try:
file = input('Please enter the name of the file you would like to open: ')
text = open(file)
break
except:
print('Please enter a valid file.')
continue
lines = text.readlines()
for line in lines:
part = line[0:8].split('/')
dd = part[0]
mm = part[1]
yy = part[2]
text = line[8:]
name = mirror(text)
print(name + " " + mm + '/' + dd + '/' + yy, end = "")
The actual results are:
senoJ moT 02/01/00
senoJ moT 02/01/00
senoJ moT 02/01/00
senoJ moT 02/01/00
senoJ moT 02/01/00senoJ moT 02/01/00
while the ones I would like are:
senoJ moT 02/01/00
senoJ moT 02/01/00
senoJ moT 02/01/00
senoJ moT 02/01/00
senoJ moT 02/01/00
senoJ moT 02/01/00
The default for the end
attribute of the print function is to add a new line character to the end of the string. With end = ""
you have told it you don't want anything on the end. Remove that and it should provide the desired output.
Also, if you're not using the with open as
convention for dealing with files, you're going to have to text.close()
when you're done with it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.