Writing CSV Files from other Files — For Data Science purpose.

VISHWAS UPADHYAYA
3 min readMar 6, 2020

So In this blog we will create CSV files from the other file.
So let’s move on topic —

Let’s see the file from which we will make the csv file.
you can download the file from here. Download the housing.data file.

So In this file there are columns of data.From this file we have to make csv file with comma separated.

The plan is —
1. we will take the each row of the file(with the help of for loop) and we will split each row from the space (by .split() method).Then we will get a list with some numbers(available in that row).

2. But In the list which we got in first step there will be some None values in the form of (‘’). We will remove these values.

3. After removing the None values.Now we will see how many columns we are going to put in csv files.So suppose that we have 14 columns now if suppose we have only 13 or anything not equal to 14 values in the list but we want only 14 values in the list because we are making 14 columns csv file.The row which don’t have 14 values we will not add that row’s values in the csv file.

Let’s execute the plan —

First we should know that how to open file So here is the code —

import csv
with open('housing.csv', 'a',newline='') as fw:
# do something with the file

So In the above code first we are opening the csv file where we are going to write data.

After this we have to open that file from where we will take data.Now the above code will become —

import csv
with open('housing.csv', 'a',newline='') as fw:
with open('housing.data', 'r',newline='') as fr:

In the above code’s third line we are opening the housing.data file.
Now we will do the same process which we discuss in the plan section on each line of the file.

So when we will iterate through file fr we will get the one line at a time.
The full code is here —

import csv
with open('housing.csv', 'a',newline='') as fw:
with open('housing.data', 'r',newline='') as fr:
for i in fr:
print(i.split(' '))
s = csv.writer(fw)
data=[float(m) for m in i.split(' ') if m]
if len(data)==14:
s.writerow([float(m) for m in i.split(' ') if m])
else:
pass

In the sixth line we are making the object of the csv.writer so that we can write row.In the seven line we are preparing list of the data(which you can understand) and after that we are checking if the data length is 14 then we are writing it in the csv file (which we have already opened as fw).

After running this code you will get the csv file of name housing.csv.

So from this csv file you can continue your any datascience project.

Thanks.

--

--

VISHWAS UPADHYAYA

Currently working on Data Science.Python Developer(Django Framework)