Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a 2d array to a csv file with delimiter [duplicate]

Tags:

python

arrays

As the title says, I want to write a 2D array to a csv file with delimiter ',' using python. My array looks like this (ndarray):

a= [[1,2,3,4],[5,6,7,8]]

and I want the output to look like:

1,2,3,4
5,6,7,8

with open('./data/positive.csv','wb') as myfile:
  wr = csv.writer(myfile) #, quoting=csv.QUOTE_ALL)
  wr.writerow(a)

How do I accomplish this?

I already have tried the solution but it doesn't work since I misplaced an 's' in my 'row' and it makes my array write to a single row instead of multiple rows.

like image 381
JefferyLR Avatar asked Jun 22 '17 06:06

JefferyLR


People also ask

How to write 2D array to CSV file in Python?

import csv array_2D = [ [3,6,9,12], [4,8,12,16]] with open ("array.csv","w+") as my_csv: newarray = csv.writer (my_csv,delimiter=',') newarray.writerows (array_2D) In the below screenshot you can see that the 2D array is stored in the csv file. You can refer to the below screenshot for the output. Python write 2Darray to CSV file

How do I open an array in a CSV file?

To open the file I have used with open (“array.csv”,”w+”) as my_csv: The array.csv is the name of the file “w+” is the mode used to write the file. Another variable new array is called as newarray = csv.writer (my_csv,delimiter=’,’). The CSV writer is used to insert the data into a CSV file.

What is a 2D array in Python?

As we know, when we talk about the 2D array, we are talking about the NumPy array. The NumPy array is basically used by computer scientists and machine learning engineers to deal with the huge amounts of data stored in the CSV file.

How do I save a NumPy array to a CSV file?

You can use the np.savetxt () method to save your Numpy array to a CSV file. If you don’t use these two settings, NumPy will save your files as .txt. More on that later. CSV files can be great because they are human-readable. They also have the added benefit of being easy to load into pandas or Dask DataFrames.


1 Answers

Use csv module by import csv also take a note you might need either writerow() or writerows().

What's the difference you may ask?

writerow takes an iterable of cells to write:

writerow([1,2,2])
->
1,2,2

writerows takes an iterable of iterables of cells to write:

writerows([[1,2,3],
           [4,5,6],
           [6,7,8]])
->
1,2,3
4,5,6
6,7,8

Which can be ofcourse be given as a variable. Like

a = [[1,2,3],[4,5,6],[6,7,8]]
writerows(a)

Conclusion writerow takes 1-dimensional data (one row), and writerows takes 2-dimensional data (multiple rows).

Program for you:

import csv

a = [[1,2,3,4],[5,6,7,8]]

with open("new_file.csv","w+") as my_csv:
    csvWriter = csv.writer(my_csv,delimiter=',')
    csvWriter.writerows(a)

Note open() takes two arguments here the file to be written and the mode.

What is a mode you may ask?

It specifies how the file should be opened. In my case w+ means open file my_file.csv if it exists if it doesn't then fine create a new one and write.

There are several modes choose one. Note: w+ overwrites everytime you use it. That is old data in file will be overwritten so if you want to just append use a. Take a look at this for more details on modes.File modes

like image 53
void Avatar answered Sep 18 '22 08:09

void