-1

I will like to convert .data file to .csv in python. Basically I want to export this .data file into my working directory as a csv file.

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/spambase/spambase.data"
raw_data = urllib.urlopen(url)
mydataset = np.loadtxt(raw_data, delimiter=",")
desertnaut
  • 57,590
  • 26
  • 140
  • 166
moski
  • 61
  • 8

1 Answers1

1

Please refer to this answer Dump a NumPy array into a csv file

Also i think you don't need to load the data in numpy array as it is already in csv format. You can just write the raw data in a .csv file.

with open('csv_file.csv', 'wb') as file:
    file.write(raw_data.read())

That should suffice.

So to sum up, If you want to use use numpy to write the csv then

import urllib.request as urllib
import numpy as np

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/spambase/spambase.data"
raw_data = urllib.urlopen(url)
mydataset = np.loadtxt(raw_data, delimiter=",")
np.savetxt("foo.csv", mydataset, delimiter=",")

And easier and faster is to just write the raw data

import urllib.request as urllib
import numpy as np

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/spambase/spambase.data"
raw_data = urllib.urlopen(url)
with open('csv_file.csv', 'wb') as file:
    file.write(raw_data.read())
Nadim Abrar
  • 186
  • 1
  • 7