Python syntaxerror: unexpected character after line continuation character

北战南征 提交于 2021-02-05 11:31:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!