Unable to read text files in python line by line

Viewed 20

I want to read python files line by line and be able to output all the data in a single string but neatly organised The data for the text file is below the code.

I want the output to be like : Name is : David Astrew. He is 185cm tall. Anime level : otaku

file_name = "uppersixx.txt"
file = open(file_name, 'r')


def ReadData():
    try:
        Name = (file.readline()).strip()
        while Name != '':
            name = file.readline().strip()
            height = (file.readline()).strip()
            state = (file.readline()).strip()
            print(f"Name is : {name}.\nHe is {height}cm tall.\n Anime level : {state}")
    except IOError:
        print("Could not find file")


ReadData()

file.close()

The text file 'uppersixx.txt' is as follows

#name
#height
#state
----------
takundamugugu
175
otaku
kesleygwangwanya
184
weeb
tapiwamusame
169
midotaku
denzelsibanda
187
normie
brightmukachana
165
normie
1 Answers

remove \n from print statements like below

print(f"Name is : {name}. He is {height}cm tall. Anime level : {state}")
Related