【问题标题】:Syntax error trying to write to a text file in python尝试在 python 中写入文本文件的语法错误
【发布时间】:2014-01-25 06:38:20
【问题描述】:

我的程序是一个完美的应用测验,在测验开始时,学生需要输入他们的:NameLastnameSchool class。在测验结束时,程序将计算分数,如果他们通过了,那么他们的详细信息将显示在屏幕上并写入文本文件。此时,用户将被要求写一个简短的陈述,说明为什么他们应该被视为级长,这也将被打印到文件上。用户应该能够将文件打印出来作为他们已通过完美应用测验的收据。

这是我的代码,但它不起作用,谁能解释为什么?

class Score_Window(tk.Toplevel):
    '''A simple instruction window'''
    def __init__(self, parent):
        tk.Toplevel.__init__(self, parent)
        score_str = str(sum(parent.score_per_question.values()))
        self.score = tk.Label(self, width=80, height=4, text = "You're score was: " + score_str)
        self.score.pack(side="top", fill="both", expand=True)


        if int(score_str) >= 3:
            print("Pass")

            self.prefect = tk.Label(self, width=80, height=4, text = "You have passed, well done! You can now become a prefect.")
            self.prefect.pack(side="top", fill="both", expand=True)

            self.name = tk.Label(self, width=80, height=4, text = student_name)
            self.name.pack(side="top", fill="both", expand=True)

            self.surname = tk.Label(self, width=80, height=4, text = student_surname)
            self.surname.pack(side="top", fill="both", expand=True)

            self.tutor = tk.Label(self, width=80, height=4, text = student_tutor_group)
            self.tutor.pack(side="top", fill="both", expand=True)

            receipt_printoff = open("data.txt", "w")
            receipt_printoff.write(student_name, " ",student_surname)
            receipt_printoff.write(student_tutor_group)
            statement = tkSimpleDialog.askstring("User data", "Enter something about yourself")
            receipt_printoff.write((" ",statement)

问题出现在下一行存在“语法”错误的地方:

        with open("data.txt", "r") as l
            for lines in l:
                student_name2, student_surname2, statement2 = lines.split()
                namelabel = label(self, text=student_name2)
                namelabel.pack()
                surnamelable = label(self, text=student_surname2)
                surnamelable.pack()
                Userinfolabel = label(self, text=statement2)
                Userinfolabel.pack()
    else:
        print("Fail")

        self.fail = tk.Label(self, width=80, height=4, text = "Unfortunately you have not scored highly enough to be considered for a prefect position.")
        self.fail.pack(side="top", fill="both", expand=True)

【问题讨论】:

  • 你忘了receipt_printoff.close(),或者在处理文件对象时最好使用with语句。
  • 那么语法错误是什么?
  • @dimo414:在 OP 认为的位置之前的那一行..
  • @MartijnPieters 是的,我可以看到,我想让 OP 告诉我们他看到了什么。
  • 什么是prefect application quiz

标签: python file text input


【解决方案1】:

您有两个语法错误:

        receipt_printoff.write((" ",statement)
        #  -----------------------------------^
    with open("data.txt", "r") as l
    # -----------------------------^

您缺少右括号) : 冒号。

第一行是导致您立即出现语法错误的原因,但是一旦您修复了它,您就会遇到第二个错误。

你可能想写一个空格和语句;为此使用字符串连接:

receipt_printoff.write(student_name + " " + student_surname)

receipt_printoff.write(" " + statement)

或使用字符串格式;您可能也想添加一些换行符:

receipt_printoff.write('{} {}\n'.format(student_name, student_surname))
# ...
receipt_printoff.write(' {}\n'.format(statement))

因为file.write()print() 完全不同;它不会为您将值转换为字符串,并且一次只能接受一个参数。

【讨论】:

    【解决方案2】:

    with open("data.txt", "r") as l 应该是 with open("data.txt", "r") as l:(注意冒号)。

    【讨论】:

      【解决方案3】:

      with open("data.txt", "r") as l: 末尾缺少一个冒号。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-09
        • 2016-02-25
        • 1970-01-01
        • 2020-08-09
        • 2022-01-23
        相关资源
        最近更新 更多