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
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,391 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 812 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  python insert blank line in logger mg24 1 2,729 Nov-02-2022, 08:36 AM
Last Post: snippsat
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,305 Sep-27-2022, 01:38 PM
Last Post: buran
  Print to a New Line when Appending File DaveG 0 1,189 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Find and delete above a certain line in text file cubangt 12 3,353 Mar-18-2022, 07:49 PM
Last Post: snippsat
  CSV to Text File and write a line in newline atomxkai 4 2,612 Feb-15-2022, 08:06 PM
Last Post: atomxkai
  writelines only writes one line to file gr3yali3n 2 2,293 Dec-05-2021, 10:02 PM
Last Post: gr3yali3n
  [Solved] Reading every nth line into a column from txt file Laplace12 7 5,142 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 1,912 Apr-06-2021, 10:08 AM
Last Post: arsentievalex

Forum Jump:

User Panel Messages

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