问题
I'm just starting python so am most likely just doing something stupid. I'm reading data off of a table and need to put them into columns in a txt file. I cannot convince my code to create a new line.
Here is my code-
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()
And the error reads-
File "PlotRdbFile.py", line 24
table.write(\nflux + " " + observed)
^
SyntaxError: unexpected character after line continuation character
Thank you in advance for finding my mistake.
回答1:
table.write(\nflux + " " + observed)
should be
table.write("\n" + flux + " " + observed)
or alternatively
table.write("\n{} {}".format(flux, observed))
More information about format() if you are curious.
来源:https://stackoverflow.com/questions/10935545/python-syntaxerror-unexpected-character-after-line-continuation-character