简体   繁体   English

\\n "行继续字符后出现意外字符"

[英]\n "unexpected character after line continuation character"

I have a recurring problem when trying to use \\n in python.尝试在 python 中使用 \\n 时,我遇到了一个反复出现的问题。 I'm trying to get the code to write to a text file after extracting data from another text file howeve when i run the code it always shows a "unexpected character after line continuation character" error after the \\n.我试图在从另一个文本文件中提取数据后将代码写入文本文件,但是当我运行代码时,它总是在 \\n 之后显示“行继续符后的意外字符”错误。 This is the code i am currently using.这是我目前正在使用的代码。

while True:
 while True:
    try:
        Prod_Code = input("enter an 8 digit number or type Done to get your final receipt: ")
        check = len(Prod_Code)
        int(Prod_Code) 

        if check == 8:
            print("This code is valid")



            with open('Data Base.txt', 'r') as searchfile:
                for line in searchfile:
                    if Prod_Code in line:
                        print(line)
                        receipt = open('Receipt.txt', 'w')
                        receipt.write(line, \n)
                        receipt.close()
                        break

        else:
            print("incorrect length, try again")

    except ValueError:
        if Prod_Code == "Done":
            print("Your receipt is being calculated")
            exit()

        else:
             print("you must enter an integer")

unlike print<\/code> , write<\/code> accepts only one argument (also you cannot write floats & ints without converting them to string - not the issue here).print<\/code>不同, write<\/code>只接受一个参数(你也不能在不将它们转换为字符串的情况下编写浮点数和整数 - 这不是这里的问题)。

And your \\n<\/code> char must be quoted of course.当然,您的\\n<\/code>字符必须被引用。 So write:所以写:

receipt.write(line + "\n")

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

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