Rohit Rohit - 25 days ago 15
Python Question

Writing to file with newlines in python 3

Hello i am running into this small issue where in i am reading some content from one file, extracting some columns and writing it into another file. Now since write() method does not add newline character after each line of text i did that via below code but that poses one problem that it would add an extra blank line at the end of the file as well and that is not intended.

fh.write(string+'\n')


So i would like to know how can we solve this, below is my code

with open("C:\\test.txt") as fh, open("C:\\newtest","w") as f:
for line in fh:
if not re.search("^$",line):
f.write(line.split()[-1].split(",")[0]+'\n')


So any suggestions.

Answer

If you know your file will always have at least one line, you could simply write the first line with no changes then write all subsequent lines with \r\n appended to the beginning of the string:

with open("C:\\test.txt") as fh, open("C:\\newtest","w") as f:
    for line in fh:
        if not re.search("^$",line):
            f.write(line.split()[-1].split(",")[0]) #first line with no newline
            break #on first occurance
    for line in fh:
        if not re.search("^$",line):
            f.write('\n'+line.split()[-1].split(",")[0]) #rest of the lines with prepended newline

edit:

why doesn't the file start back on the first line with the second loop?

behind the scenes, the object fh has some internal state to keep track of effectively a "cursor" within the file, and a special method called fh.next() this method is used to yield the next value (in this case each line as separated by '\n'. when the end of the file is reached a special type of exception is raised called StopIteration this is a special exception type recognized by the for loop that tells it to exit the loop. If the loop is exited beforehand using break, the internal cursor in the file stays in place, and further iteration picks up where you left off.

you can play around with learning how iteration works behind the scenes by creating your own custom generator and looping over it with a for loop:

def generator_constructor():
    x = 10
    while x > 0:
        yield x
        x = x - 1 #decrement x

generator = generator_constructor()

print generator.next() #prints 10
print generator.next() #prints 9

print "\nlooping\n" #indicate where we enter the loop

while True: #infinite loop we will need to break out of somehow
    try:
        print generator.next() #print next value
    except StopIteration: #if we reach the end (exit wile loop of generator constructor)
        break #then break out of the loop

Try taking this code and making it do something more interesting so you can understand what's going on behind the scenes

Comments