E
E
EVGenyK_ch2021-01-26 20:03:31
csv
EVGenyK_ch, 2021-01-26 20:03:31

How to split csv file line by line into many files?

There is a file in csv format, let's say it has 1000 lines and 2 columns. Q: How can this file be divided into 1000 files, i.e. line by line?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2021-01-26
@hottabxp

It is possible in Python:

with open('data.csv') as file:
  lines = file.read().splitlines()

def write_line(filename, line):
  with open(filename+'.csv','w') as file:
    file.write(line)

for i,line in enumerate(lines):
  write_line(str(i),line)

This script reads the data.csv file and writes each line to a new file - 0.csv, 1.csv, 2.csv, and so on until the end of the input file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question