Saturday 30 January 2021

newline='' doesn't seem to work for me alone

First off, this is annoying question and also I request everyone reading this to read fully. This is an issue faced by me alone not by anyone else.

I've a csv file as follows:

1,a
2,b
3,c
4,d

I want to modify this file using Python 3.7.2 in my Windows 7.

import csv
row = [5,'e']
with open('modify_existing.csv')as readFile:
    reader= csv.reader(readFile)
    lines = list(reader)
    lines[3] = row
with open('modify_existing.csv','w',newline='') as writeFile:
    writer = csv.writer(writeFile)
    writer.writerows(lines)
print(lines)

I can assure you that except me, for all others the output is correct I don't know why my output is wrong. the newline parameter doesn't seem to work at all.

And the annoying thing, the output I get is not consistent. I get blank lines when executing for the first time and three blank lines when executing second time. Sometime, I didn't get blank lines at all. And furthermore, I get blank lines at the first time and not the second time.

I am actually fed up with this error. I don't know where to ask this. Sorry if I am asking something that doesn't happen for you. Even for me, in my other laptop having the same Windows 7 (64 bit) with the same Python 3.7.2, no blank lines appear.

But only in my laptop it occurs. For instance, I completely deleted the Python and installed 3.7.2 again. Same result.

Could anyone please help me with this issue?

EDIT : When I print that readfile, I get the correct output. When there are blank lines in my csv file (first or second time when it appears). I can assure you that the mistake in writing the csv file



from newline='' doesn't seem to work for me alone

No comments:

Post a Comment