我刚刚开始使用python,所以我很可能只是在做一些愚蠢的事情。我正在从表中读取数据,需要将它们放入txt文件中的列中。我无法说服我的代码创建一个新行。
这是我的代码 -
file = open("test_m.rdb")
table = open('table.txt', 'w+')
trash = file.readline()
trash = file.readline()
data = file.readline()
i = data.split()
flux = i[2]
observed = i[4]
table.write(flux + " " + observed,)
while 1:
line = file.readline()
i = line.split()
try:
flux = i[2]
observed = i[4]
except IndexError:
break
table.write(\nflux + " " + observed)
table.close()
错误显示为 -
File "PlotRdbFile.py", line 24
table.write(\nflux + " " + observed)
^
SyntaxError: unexpected character after line continuation character
提前感谢您发现我的错误。
答案 0 :(得分:5)
table.write(\nflux + " " + observed)
应该是
table.write("\n" + flux + " " + observed)
或者
table.write("\n{} {}".format(flux, observed))
如果您感到好奇,请提供有关format()的更多信息。