简体   繁体   English

如何使用python在已经存在的文本文件中写入标题

[英]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.或者,如果您只需要像标题一样的东西,那么您需要确定页面垂直适合多少个字符,然后每 N 行打印标题。

For horizontal alignment, make use of the extra tokens you can use with format().对于水平对齐,请使用可以与 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.其中 ^8 表示我希望字符串以 8 个字符为中心。 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.如果尚未创建mytextfile.txt ,则会抛出错误。 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).如果您已经创建了空的mytextfile.txt ,那么它仍然会抛出错误,因为没有lines[0] (它是一个空文件)。

And your if statement has a problem too, you should write:而且你的if语句也有问题,你应该写:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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