View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Comma Separated Values [CSV] files a type of a plain text document in which tabular information is structured using a particular format. A CSV file is a bounded text format which uses a comma to separate values. The most common method to write data from a list to CSV file is the writerow[] method of writer and DictWriter class. Example 1: Creating a CSV file and writing data row-wise into it using writer class.
Python3
import
csv
data
=
[[
'Geeks'
], [
4
], [
'geeks !'
]]
file
=
open
[
'g4g.csv'
,
'w+'
, newline
=
'']
with
file
:
write
=
csv.writer[
file
]
write.writerows[data]
Output:
Python3
import
csv
file
=
open
[
'g4g.csv'
,
'w'
, newline
=
'']
with
file
:
header
=
[
'Organization'
,
'Established'
,
'CEO'
]
writer
=
csv.DictWriter[
file
, fieldnames
=
header]
writer.writeheader[]
writer.writerow[{
'Organization'
:
'Google'
,
'Established'
:
'1998'
,
'CEO'
:
'Sundar Pichai'
}]
writer.writerow[{
'Organization'
:
'Microsoft'
,
'Established'
:
'1975'
,
'CEO'
:
'Satya Nadella'
}]
writer.writerow[{
'Organization'
:
'Nokia'
,
'Established'
:
'1865'
,
'CEO'
:
'Rajeev Suri'
}]
Output:
Python3
import
csv
data
=
[[
'Geeks for Geeks'
,
'2008'
,
'Sandeep Jain'
],
[
'HackerRank'
,
'2009'
,
'Vivek Ravisankar'
]]
file
=
open
[
'g4g.csv'
,
'a+'
, newline
=
'']
with
file
:
write
=
csv.writer[
file
]
write.writerows[data]
Output: