简体   繁体   中英

how to write a header in already existent text file with python

I created a function that appends student's data to a text file. After creating that function I wanted to add a header so that the user would know what the data represents. However now, the header is indeed added to the file, but the function does not add the student's data anymore...

Here is my code:

FirstName =  []
LastName = []
Class = []
Adress = []
Math = []
Science = []
English = []
Dutch = []
Arts = []



def add_records(filename, FirstName, LastName, Class, Adress, Math, Science, English, Dutch, Arts):


    header = "First Name, Last Name, Class, Adress, Math Grade, Science Grade, English Grade, Dutch Grade, Arts Grade"

    x= input("Enter First Name:")
    FirstName.append(x)


    y=input("Enter Last Name:")
    LastName.append(y)

    b=input("Enter Student's Class:")
    Class.append(b)

    o=input("Enter Address:")
    Address.append(o)


    z=int(input("Enter Math Grade:"))
    Math.append(z)


    w=int(input("Enter Science Grade:"))
    Science.append(w)


    h=int(input("Enter English Grade:"))
    English.append(h)


    p=int(input("Enter Dutch Grade:"))
    Dutch.append(p)

    v=int(input("Enter Arts Grade:"))
    Arts.append(v)



    f = open(filename, 'r')
    lines = f.readlines()


    if lines[0] in f == header:

        f=open(filename,"a+")




        for i, j, r, k ,l, m, n, o, p  in zip(FirstName, LastName, Class, Adress, Math, Science, English, Dutch, Arts):

                print(i,j,k,r,l,m,n,o,p)
                a=f.write(i + ' ' + j + ' ' + r + ' '+ k + ' ' + str(l) + ' ' + str(m) + ' ' + str(n) + ' ' + str(o) + ' ' + str(p) + "\n")

        f.close()

    else:

        file = open(filename, 'a+')
        file.write(header + a + "\n")
        file.close()
        f.close()
add_records("mytextfile.txt",FirstName,LastName,Class,Adress,Math,Science,English,Dutch,Arts)

Could someone explain to me why?

text files do not have a header. If you want a true header, you'll need a more complex format. Alternatively, if you just need something that acts like a header, then you need to figure out how many characters fit on your page vertically, and print the header every N lines.

For horizontal alignment, make use of the extra tokens you can use with format(). As an example:

print('{a:^8}{b:^8}{c:^8}'.format(a='this', b='that', c='other'))
this    that   other 

where ^8 says I want the string centered across 8 characters. Obviously you have to choose (or derive) the value that works for your data.

In case the mytextfile.txt has not been created yet, it will throw an error. If you already created the empty mytextfile.txt , then it still throws an error because there is no lines[0] (it's an empty file).

And your if statement has a problem too, you should write:

if lines[0].strip() == header

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM