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?
#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


Messages In This Thread
RE: Why there's a 'blank line' on CSV file? - by snippsat - Mar-24-2021, 10:55 PM

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