如何换行

时间:2019-03-28 20:49:03

标签: python

如何在输出文件上打印新行?
当我尝试使用“ / n”添加新行时,它只会打印/ n

这是我到目前为止所拥有的。

`` inputFile = open(“ demofile1.txt”,“ r”) outFile = open(“ Ji

    string = line.split(',')

    go =(string)[3::]
    bo = [float(i) for i in go]
    total = sum(bo)
    pine = ("%8.2f"%total)
    name = string[2] + "," + " " + string[1]

    kale = (string[0] + " " + name + " " + "/n")

    se)

当前结果


8 
53 Baul 
A999999

1 个答案:

答案 0 :(得分:8)

您需要使用\n,而不是/n。所以这行:

kale = (string[0] + " " + name + " " + "/n")

应该是:

kale = (string[0] + " " + name + " " + "\n")

此外,请务必考虑使用str格式化程序,因此所有这些行:

go =(string)[3::]
bo = [float(i) for i in go]
total = sum(bo)
pine = ("%8.2f"%total)
name = string[2] + "," + " " + string[1]
kale = (string[0] + " " + name + " " + "/n")
str1 = ''.join(kale)
str2 = ''.join(pine)
outFile.write(str1 + " " + str2 + " ")

将成为:

outFile.write("{} {} {:8.2f}\n".format(string[0], string[2] + ", " + string[1], sum(bo))