Overview of CSV Module¶
Let us get an overview of CSV Module. It provide some wrapper functions to provide structure to the delimited data in collections as well as to write the collections back in the form of delimited records.
- You need to import
csv
module to use the functions that are part of it. - It exposes several APIs to deal with collections which contain delimited strings. Most of them are to read and write.
In [1]:
import csv
- Let us see how we can read the list with delimited strings using
csv.reader
.
In [2]:
l = ['1,Scott,Tiger', '2,Donald,Duck']
In [3]:
csv.reader?
Docstring: csv_reader = reader(iterable [, dialect='excel'] [optional keyword args]) for row in csv_reader: process(row) The "iterable" argument can be any object that returns a line of input for each iteration, such as a file object or a list. The optional "dialect" parameter is discussed below. The function also accepts optional keyword arguments which override settings provided by the dialect. The returned object is an iterator. Each iteration returns a row of the CSV file (which can span multiple input lines). Type: builtin_function_or_method
In [4]:
csv.reader(l)
Out[4]:
<_csv.reader at 0x7fc56e62f890>
In [5]:
list(csv.reader(l))
Out[5]:
[['1', 'Scott', 'Tiger'], ['2', 'Donald', 'Duck']]
In [6]:
for item in list(csv.reader(l)):
print(item)
['1', 'Scott', 'Tiger'] ['2', 'Donald', 'Duck']
In [7]:
l = ['1\tScott\tTiger', '2\tDonald\tDuck']
In [8]:
list(csv.reader(l, delimiter='\t'))
Out[8]:
[['1', 'Scott', 'Tiger'], ['2', 'Donald', 'Duck']]
In [9]:
lt = [(1, 'Scott', 'Tiger'), (2, 'Donald', 'Duck')]
Now let us see how we can write list of tuples to the file using csv.writer
.
- First we need to create file object in write mode.
- We need to create csv writer object using
csv.writer
. By default, the object will be created with,
as delimiter or separator. - We can pass key word argument
delimiter
along with the character using which we want the data to be separated by using specific character.
In [10]:
csv.writer?
Docstring: csv_writer = csv.writer(fileobj [, dialect='excel'] [optional keyword args]) for row in sequence: csv_writer.writerow(row) [or] csv_writer = csv.writer(fileobj [, dialect='excel'] [optional keyword args]) csv_writer.writerows(rows) The "fileobj" argument can be any object that supports the file API. Type: builtin_function_or_method
In [11]:
!mkdir -p data/users
In [12]:
!rm -f data/users/part-00000
In [13]:
users_file = open('data/users/part-00000', 'w')
In [14]:
csv_writer = csv.writer(users_file)
In [15]:
csv_writer
Out[15]:
<_csv.writer at 0x7fc56e6b69a0>
In [16]:
csv_writer.writerows(lt)
In [17]:
users_file.close()
In [18]:
!ls -ltr data/users/part-00000
-rw-r--r-- 1 itversity itversity 30 Mar 25 06:13 data/users/part-00000
In [19]:
!cat data/users/part-00000
1,Scott,Tiger 2,Donald,Duck
- Let us see an example to write the data to the file using
|
as delimiter.
In [20]:
!mkdir -p data/users
In [21]:
!rm -f data/users/part-00000
In [22]:
users_file = open('data/users/part-00000', 'w')
In [23]:
csv_writer = csv.writer(users_file, delimiter='|')
In [24]:
csv_writer.writerows(lt)
In [25]:
users_file.close()
In [26]:
!ls -ltr data/users/part-00000
-rw-r--r-- 1 itversity itversity 30 Mar 25 06:13 data/users/part-00000
In [27]:
!cat data/users/part-00000
1|Scott|Tiger 2|Donald|Duck