0

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

1 Answer 1

5
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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I have a followup question. I tried table.write("\n" + flux + " " + observed) before I posted this question. I thought it wasn't working properly because when I open up the txt file with notepad, it puts all the data on one line. When I tried it a second time after you answered my question, I opened it with Word and it is formatted correctly but it still isn't formatted correctly when I open the file in Notepad. Do you know why that is?
And thank you for the format() link. It's going to be very helpful.
@Elerrina different word processor/editors will display data sometimes differently unfortunately (mostly a matter how they handle newlines). I use emacs, probably not the most user-friendly editor for my programming, not sure what I can recommend to you. I've heard good things about notepad++ notepad-plus-plus.org maybe you can check it out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.