Python Forum
csv module writing extra line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
csv module writing extra line
#1
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
Reply
#2
https://stackoverflow.com/a/3348664/4046632
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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.
Reply
#4
(Jun-05-2018, 12:21 PM)ChipsSlave Wrote: My raspberry is running python version 2.
more important - it's not running windows :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,566 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,377 Sep-27-2022, 01:38 PM
Last Post: buran
  ModuleNotFoundError: No module named 'plotly.validators.scatter3d.line' jeffbrownell 3 4,851 Jul-09-2020, 08:15 AM
Last Post: snippsat
  Keyboard Module Python - Suppress input while writing to window ppel123 0 2,800 Apr-08-2020, 02:51 PM
Last Post: ppel123
  Avoiding empty line in writing process csv file go127a 4 9,707 May-10-2019, 01:33 PM
Last Post: go127a
  Mysterious Extra New-Line trevorkavanaugh 2 2,497 Feb-27-2019, 07:13 PM
Last Post: trevorkavanaugh
  An Extra 'None' leoahum 5 3,892 Oct-18-2018, 08:20 PM
Last Post: volcano63
  python export to csv writes extra line between rows jahjahcity 4 10,352 Jul-25-2018, 01:36 AM
Last Post: jahjahcity
  Writing values at a desired column in a line of text file Gupta 3 3,471 Jul-28-2017, 11:08 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020