gpt4 book ai didi

python - Python语法错误: unexpected character after line continuation character

转载 作者:行者123 更新时间:2023-12-03 08:14:45 60 4
gpt4 key购买 nike

我刚开始使用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

预先感谢您发现我的错误。

最佳答案

table.write(\nflux + " " + observed)

应该
table.write("\n" + flux + " " + observed)

或者
table.write("\n{} {}".format(flux, observed))

如果您好奇的话,有关 format()的更多信息。

关于python - Python语法错误: unexpected character after line continuation character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10935545/

60 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com