Numpy savetxt to append to existing CSV file

Python

In this example, we are going to learn Numpy savetxt to append to existing CSV files. These can be used to append to existing text files by changing the file extension to .txt.

Numpy savetxt() method


We are using the NumPy module savetxt() method which allows us to save text and csv files.

numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ', encoding=None)

Parameters :

  • fname : The name of file we want to create txt/csv.
  • fmt = The format patten or list of pattern we will use while write file content.
    • if single format pattern is gievn that it will apply to all content like fmt=%s in below example
    • The list of format pattern can be used for each column
  • delimiter : To string or charcater for csv file element separtor.
  •  header : parameter to pass columns name or header row of CSV file.
  • deleimiter: is used to separate the row value by comma(,).
  • comments=” : to elminate the # by default added to first column name.
  • footer : The string to write at end of file.

1. Numpy savetxt to append to existing CSV file


The NumPy module savetxt() method is used to append the Numpy array at the end of the existing csv file with a single format pattern fmt = %s.

  • We will open CSV file in append mode and call the savetxt() method to writes rows with header.
  • Second call the savetxt() method to append the rows.
  • List_rows : It contain a list of lists to be written to CSV file rows
  • list_row_append : It conatins rows to be append end of csv file.
  • np_array: It will convert List_rows to numpy array that will use to write rows of csv file.
  • np_array_append: It will convert list_row_append to numpy array will append rows at end of existing csv.

Python Program

import numpy as np


List_rows = [['Tom', 9, 8, 'WA'],['Trex', 6, 15, 'CA'],  ['Kity', 7, 11, 'WA']]

list_row_append = [ ['Rack', 9, 8, 'WA'],  ['Sack', 6, 15, 'CA'],  ['Mack', 7, 11, 'WA']]

np_array_rows = np.array(List_rows)

np_array_rows_append = np.array(list_row_append)


  

with open('animal.csv','a') as csvfile:
 np.savetxt(csvfile, np_array_rows,delimiter=',',header='Name, Age, Weight, City',fmt='%s', comments='')
 np.savetxt(csvfile,np_array_rows_append,delimiter=',',fmt='%s', comments='')

Output

Name	 Age	 Weight	 City
Tom	9	8	WA
Trex	6	15	CA
Kity	7	11	WA
Rack	9	8	WA
Sack	6	15	CA
Mack	7	11	WA

2. Numpy savetxt to append to existing CSV file by specific format pattern


The NumPy module savetxt() method to append Numpy array row at the end of the existing csv file. We are using the specific format to save the numpy array to csv.

  • We will open CSV file in append mode and call the savetxt() method to writes rows with header
  • Second call the savetxt() method to append the rows.
  • List_rows : It contain a list of tuples to be written to CSV file rows
  • list_row_append : It conatins a list of tuples to be append end of csv file np_array: It will convert List_rows to numpy array that will use to write rows of csv file.
  • np_array_append: It will convert list_row_append to numpy array will append rows at end of existing csv.

Let us understand with the below example how to use Numpy savetxt() to append

Python Program Example

import numpy as np

list_rows= [ ('Tom', 9, 8, 'WA'),  ('Trex', 6, 15, 'CA'),  ('Kity', 7, 11, 'WA')]
list_row_append = [ ('Rack', 9, 8, 'WA'),  ('Sack', 6, 15, 'CA'),  ('Mack', 7, 11, 'WA')]


 
dtype = [('Name', (np.str_, 10)), ('Marks', np.int32), ('weight', np.int32),('City', (np.str_, 5))]
 
np_array = np.array(list_rows, dtype=dtype)

np_array_append = np.array(list_row_append, dtype=dtype)
  

with open('animal.csv','a') as csvfile:
 np.savetxt(csvfile, np_array,delimiter=',',header='Name, Age, Weight, City',fmt=['%s' , '%f', '%d','%s'], comments='')
 np.savetxt(csvfile,np_array_append,delimiter=',',fmt=['%s' , '%f', '%d','%s'], comments='')

Output

Name	Marks Age city
Tom	     9	  8	  WA
Trex	6	15	CA
Kity	7	11	WA
Rack	9	8	WA
Sack	6	15	CA
Mack	7	11	WA

Summary

In this post, we have learned how to Numpy savetxt to append to existing CSV file or these ways can be used for text file by using following ways:

  • Numpy savetxt to append to existing CSV file
  • Numpy savetxt to append to existing CSV file with specific format pattern