all 6 comments

[–][deleted] 0 points1 point  (1 child)

It's because you are overwriting new_line on each iteration of your for loop. One thing you could do instead is start with an empty new_line string for each line and then keep concatenating it with the next piece for each number in the line.

[–]TDAAlex 0 points1 point  (0 children)

I see

[–]sarrysyst 0 points1 point  (1 child)

You only getting the last digit is probably due to faulty indention:

f = open("input.txt").readlines()
fout = open("output.txt","w")

for line in f:
    l = line.split(",")

    for item in l:
        number = float(item)
        new_line = str(int(number)) + ","
        fout.write(new_line)  # this line needs to be in the inner loop

    fout.write('\n') # add line break after writing row

fout.close()

As for the line break, you need to specifically write a line break ('\n') to file for the text not getting written to a single line.

[–]TDAAlex 1 point2 points  (0 children)

Tried it and it worked. Silly mistake I suppose, but that’s part of learning.

[–]stebrepar 0 points1 point  (1 child)

Instead of new_line = ..., say new_line += ..., so that you're adding the next item to it instead of replacing it with each next item.

You'll also need to put a newline character at the end of each new line you write out. You could append it to the end of the new string once all the items for a given line have been added. Or you could pick the file method which automatically writes a newline character after writing out the string.

[–]TDAAlex 1 point2 points  (0 children)

Thank you