Why there's a 'blank line' on CSV file? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Why there's a 'blank line' on CSV file? (/thread-33046.html) |
Why there's a 'blank line' on CSV file? - brunolelli - Mar-24-2021 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 1But 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 1What's going on? How can I fix it, and get the values on my csv file without the blank line? Thank you! RE: Why there's a 'blank line' on CSV file? - snippsat - Mar-24-2021 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)
RE: Why there's a 'blank line' on CSV file? - brunolelli - Mar-24-2021 (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. 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 RE: Why there's a 'blank line' on CSV file? - brunolelli - Mar-24-2021 (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. I found a way to solve my issu.... Saving as TXT! RE: Why there's a 'blank line' on CSV file? - buran - Mar-25-2021 You are on Windows, open the out file with newline='' . It is in the docs. See also https://stackoverflow.com/a/3348664/4046632
|