Python Forum
Why there's a 'blank line' on CSV file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why there's a 'blank line' on CSV file?
#1
Exclamation 
Hello guys,

With the following code I'm exporting my data to a CSV file:
from tempfile import NamedTemporaryFile
from FlightRadar24.api import FlightRadar24API
from time import time, sleep
from datetime import datetime
import shutil
import csv
import threading
import schedule 
import time 
import sched, time

s = sched.scheduler(time.time, time.sleep)
def do_something(sc): 
    print("Doing stuff...")

    now = datetime.now()
    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
    company1 = len(fr_api.get_flights(airline = 'AZU'))
    company2 = len(fr_api.get_flights(airline = 'GLO'))
    company3 = len(fr_api.get_flights(airline = 'LAN'))
    company4 = len(fr_api.get_flights(airline = 'PTB'))
    
    print(dt_string, company1, company2, company3, company4)
    fields=[dt_string,company1,company2,company3,company4]


    with open(r'C:\Users\bruno\Desktop\Voos\Voos.csv', 'a') as f:
        writer = csv.writer(f, delimiter=';')
        writer.writerow(fields)   

    
    s.enter(60, 1, do_something, (sc,))

s.enter(60, 1, do_something, (s,))
s.run()
When I print it, I get the values line by line...
print(dt_string, company1, company2, company3, company4)


23/03/2021 09:09	50	18	34	1				
23/03/2021 09:09	50	18	34	1				
23/03/2021 09:09	50	19	34	1				
23/03/2021 09:09	50	19	34	1				
23/03/2021 09:09	50	19	34	1				
23/03/2021 09:09	51	19	34	1				
23/03/2021 09:09	51	19	34	1				
23/03/2021 09:09	51	19	34	1
But when I open my csv file, the results are separated by an empty line... like this:
23/03/2021 09:09	50	18	34	1
				
23/03/2021 09:09	50	18	34	1
				
23/03/2021 09:09	50	19	34	1
				
23/03/2021 09:09	50	19	34	1
				
23/03/2021 09:09	50	19	34	1
				
23/03/2021 09:09	51	19	34	1
				
23/03/2021 09:09	51	19	34	1
				
23/03/2021 09:09	51	19	34	1
What's going on?
How can I fix it, and get the values on my csv file without the blank line?

Thank you!
Reply
#2
If i just copy some of your data,then doing some stuff to get it in a list data structure.
You can append to list so get similar structure,then it easy to write to csv.
import csv

data = '''\
23/03/2021 09:09    50  18  34  1
23/03/2021 09:09    50  18  34  1
23/03/2021 09:09    50  19  34  1
23/03/2021 09:09    50  19  34  1'''

lst = data .split('\n')
lst = [i.split() for i in lst]
print(lst)

with open("out.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(lst)
Output:
[['23/03/2021', '09:09', '50', '18', '34', '1'], ['23/03/2021', '09:09', '50', '18', '34', '1'], ['23/03/2021', '09:09', '50', '19', '34', '1'], ['23/03/2021', '09:09', '50', '19', '34', '1']] # out.csv 23/03/2021,09:09,50,18,34,1 23/03/2021,09:09,50,18,34,1 23/03/2021,09:09,50,19,34,1 23/03/2021,09:09,50,19,34,1
Reply
#3
(Mar-24-2021, 10:55 PM)snippsat Wrote: If i just copy some of your data,then doing some stuff to get it in a list data structure.
You can append to list so get similar structure,then it easy to write to csv.
import csv

data = '''\
23/03/2021 09:09    50  18  34  1
23/03/2021 09:09    50  18  34  1
23/03/2021 09:09    50  19  34  1
23/03/2021 09:09    50  19  34  1'''

lst = data .split('\n')
lst = [i.split() for i in lst]
print(lst)

with open("out.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(lst)
Output:
[['23/03/2021', '09:09', '50', '18', '34', '1'], ['23/03/2021', '09:09', '50', '18', '34', '1'], ['23/03/2021', '09:09', '50', '19', '34', '1'], ['23/03/2021', '09:09', '50', '19', '34', '1']] # out.csv 23/03/2021,09:09,50,18,34,1 23/03/2021,09:09,50,18,34,1 23/03/2021,09:09,50,19,34,1 23/03/2021,09:09,50,19,34,1

Thank you for your assistance, but I don't think it will work...
As you can see, I'm writing my data from this code
fields=[dt_string,company1,company2,company3,company4]
And it's already a list...
And I'm basically printing or writing line by line of this list, because purely it's a list with just one line that I'm writing every second basically
Reply
#4
(Mar-24-2021, 11:00 PM)brunolelli Wrote:
(Mar-24-2021, 10:55 PM)snippsat Wrote: If i just copy some of your data,then doing some stuff to get it in a list data structure.
You can append to list so get similar structure,then it easy to write to csv.
import csv

data = '''\
23/03/2021 09:09    50  18  34  1
23/03/2021 09:09    50  18  34  1
23/03/2021 09:09    50  19  34  1
23/03/2021 09:09    50  19  34  1'''

lst = data .split('\n')
lst = [i.split() for i in lst]
print(lst)

with open("out.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(lst)
Output:
[['23/03/2021', '09:09', '50', '18', '34', '1'], ['23/03/2021', '09:09', '50', '18', '34', '1'], ['23/03/2021', '09:09', '50', '19', '34', '1'], ['23/03/2021', '09:09', '50', '19', '34', '1']] # out.csv 23/03/2021,09:09,50,18,34,1 23/03/2021,09:09,50,18,34,1 23/03/2021,09:09,50,19,34,1 23/03/2021,09:09,50,19,34,1

Thank you for your assistance, but I don't think it will work...
As you can see, I'm writing my data from this code
fields=[dt_string,company1,company2,company3,company4]
And it's already a list...
And I'm basically printing or writing line by line of this list, because purely it's a list with just one line that I'm writing every second basically

I found a way to solve my issu.... Saving as TXT!
Reply
#5
You are on Windows, open the out file with newline=''. It is in the docs. See also 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


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Solved] Reading every nth line into a column from txt file Laplace12 7 790 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 465 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  Read/Sort Large text file avoiding line-by-line read using mmep or hdf5 Robotguy 0 701 Jul-22-2020, 08:11 PM
Last Post: Robotguy
  Searching string in file and save next line dani8586 2 738 Jul-10-2020, 09:03 AM
Last Post: dani8586
  Ignore first few letters of a line when reading file. ShakeyPakey 16 2,248 May-30-2020, 02:17 PM
Last Post: BitPythoner
  Delete all contents of a file from the fifth line? PythonNPC 1 817 Apr-18-2020, 09:16 AM
Last Post: buran
  Read a CSV file only from the 4th line/row asaforen7 8 1,813 Apr-16-2020, 01:49 AM
Last Post: Larz60+
  Deleting a line in a csv file julio2000 6 1,335 Feb-25-2020, 05:12 PM
Last Post: julio2000
  CSV gives me blank row on PC, but not a Mac bazcurtis 2 1,038 Jan-06-2020, 08:40 AM
Last Post: buran
  Detect end of line in text file including line breaks DanielM 4 1,339 Dec-18-2019, 11:57 AM
Last Post: Malt

Forum Jump:

User Panel Messages

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