How to read/write CSV files in Python
csv
13
Python
82
read
6
write
5
Male avatar

loveprogramming viết ngày 16/05/2021

https://grokonez.com/python/how-to-read-write-csv-files-in-python-csv-module

How to read/write CSV files in Python

In this tutorial, we're gonna look at way to use csv module to read, write CSV files in Python program.

CSV files

CSV stands for 'comma-separated values'. CSV file is a spreadsheet stored as plain-text file.
Each line in a CSV file represents a row, and commas separate cells in the row.

For example:


Title,Category,Date
Python String methods,Python,12/27/2018
How to iterate over a List in Java,Java,12/21/2018
Kotlin List functions,Kotlin,12/19/2018

Instead of commas, cells could be separated with tab characters:


Title   Tags    Date
Python String methods   python, string  12/27/2018
How to iterate over a List in Python    python, list    12/21/2018
Python List functions   python, list    12/19/2018

CSV file is simple than Excel spreadsheet because it doesn't have:

  • types, everything is just a string
  • font size or color
  • multiple worksheets
  • cell widths and heights
  • merge cells
  • images or charts

    Read CSV file in Python

    You can follow these steps to read data from a CSV file:
  • import csv module
  • open CSV file using open() function
  • create a Reader object that lets you iterate over rows
  • access values in the Reader object by:
  • convert it to a plain Python list using list() function
  • use for-loop (avoid loading the entire file into memory at once)
    
    # cells are separated with commas
    >>> import csv
    >>> gkzFile = open('grokonez.csv')
    >>> csvReader = csv.reader(gkzFile)
    >>> gkzTuts = list(csvReader)
    >>> gkzTuts
    [['Title', 'Category', 'Date'], ['Python String methods', 'Python', '12/27/2018'], ['How to iterate over a List in Java', 'Java', '12/21/2018'], ['Kotlin List functions', 'Kotlin', '12/19/2018']]
    >>> gkzTuts[0][0] # data[row][col]
    'Title'
    >>> gkzTuts[0][1]
    'Category'
    >>> gkzTuts[0][2]
    'Date'
    >>> gkzTuts[2][0]
    'How to iterate over a List in Java'
    >>> gkzTuts[2][1]
    'Java'
    >>> gkzTuts[2][2]
    '12/21/2018'

cells are separated with 'tab' characters

gkzFile = open('grokonez.csv')
csvReader = csv.reader(gkzFile, delimiter='\t')
gkzTuts = list(csvReader)
gkzTuts
[['Title', 'Tags', 'Date'], ['Python String methods', 'python, string', '12/27/2018'], ['How to iterate over a List in Python', 'python, list', '12/21/2018'], ['Python List functions', 'python, list', '12/19/2018']]
gkzTuts[0][0]
'Title'
gkzTuts[0][1]
'Tags'
gkzTuts[0][2]
'Date'
gkzTuts[2][0]
'How to iterate over a List in Python'
gkzTuts[2][1]
'python, list'
gkzTuts[2][2]
'12/21/2018'

Remember that we can loop over Reader object only once.
To reread the CSV file by for-loop, we must create Reader object again.

More at:

https://grokonez.com/python/how-to-read-write-csv-files-in-python-csv-module

How to read/write CSV files in Python

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

545 bài viết.
97 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 11 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 9 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 9 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/kotlin/kotlinreadwritecsvfileexample How to read/write CSV file in Kotlin In this tutorial, we're gonna look at examples tha...
loveprogramming viết 5 tháng trước
0 0
Male avatar
0 0
https://grokonez.com/python/howtoreadwriteexcelfilesinpythonopenpyxlmodule How to read/write Excel files in Python In this tutorial, we're gonna...
loveprogramming viết 4 tháng trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
545 bài viết.
97 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!