Python Forum

Full Version: csv module writing extra line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,

I have written a small script which is supposed to read from the file the last row and return it as a starting point for a while loop to start calculating prime numbers. The problem I am having is that if I run the code in Windows environment on Spyder the script writes an extra line in the file and it looks like this:

Row number, Prime number
1, 3
2,
3, 5
4,
5, 7
...and so on.

If I run the very same code on my raspberry application called Thonny, csv file is written with no lines skipped.

Here is my script:
import csv

division_number = 2

csv_rows = []
with open('prime2.csv', 'r') as datafile:
    for line in datafile:
        csv_rows.append(line.strip())
        
number_checking = int(csv_rows[-2])

while division_number < number_checking:
    remainder = number_checking % division_number
    if remainder == 0:
        number_checking += 1
        division_number = 2
    else:
        division_number += 1
        if division_number == number_checking:
            print(number_checking)
            with open('prime2.csv','a') as csv_file:
                writer = csv.writer(csv_file)
                writer.writerow([number_checking])
            number_checking += 1
            division_number = 2
(Jun-05-2018, 12:12 PM)buran Wrote: [ -> ]https://stackoverflow.com/a/3348664/4046632

Thank you, I see it now. My raspberry is running python version 2.
(Jun-05-2018, 12:21 PM)ChipsSlave Wrote: [ -> ]My raspberry is running python version 2.
more important - it's not running windows :-)