How to Read Csv in Python as Numbers
Summary: in this tutorial, you'll learn how to read a CSV file in Python using the built-in csv
module.
What is a CSV file
CSV stands for comma-separated values. A CSV file is a delimited text file that uses a comma to split up values.
A CSV file consists of ane or more lines. Each line is a data record. And each information tape consists of one or more than values separated by commas. In addition, all the lines of a CSV file have the same number of values.
Typically, you use a CSV file to shop tabular data in plain text. The CSV file format is quite popular and supported by many software applications such as Microsoft Excel and Google Spreadsheet.
Reading a csv file in Python
To read a CSV file in Python, you follow these steps:
First, import the csv module:
import csv
Code language: Python ( python )
Second, open the CSV file using the born open() function in the read mode:
f = open('path/to/csv_file')
Code language: Python ( python )
If the CSV contains UTF8 characters, you need to specify the encoding like this:
f = open('path/to/csv_file', encoding='UTF8')
Lawmaking linguistic communication: Python ( python )
Third, pass the file object (f
) to the reader()
function of the csv
module. The reader()
role returns a csv reader object:
Code language: Python ( python )
csv_reader = csv.reader(f)
The csv_reader
is an iterable object of lines from the CSV file. Therefore, y'all can iterate over the lines of the CSV file using a for
loop:
for line in csv_reader: impress(line)
Code linguistic communication: Python ( python )
Each line is a list of values. To access each value, you utilise the square bracket annotation []
. The first value has an index of 0. The 2nd value has an index of 1, and so on.
For example, the following accesses the first value of a particular line:
line[0]
Code linguistic communication: Python ( python )
Finally, e'er close the file once y'all're no longer access it by calling the shut()
method of the file object:
Code language: Python ( python )
f.close()
It'll be easier to apply the with
argument so that you don't demand to explicitly call the close()
method.
The post-obit illustrates all the steps for reading a CSV file:
import csv with open('path/to/csv_file', 'r') as f: csv_reader = csv.reader(f) for line in csv_reader: # process each line print(line)
Code language: Python ( python )
Reading a CSV file examples
We'll use the land.csv
file that contains country information including name, area, 2-letter state code, 3-letter of the alphabet country code:
Download state.csv file
The post-obit shows how to read the country.csv
file and display each line to the screen:
import csv with open up('land.csv', encoding="utf8") as f: csv_reader = csv.reader(f) for line in csv_reader: impress(line)
Lawmaking language: Python ( python )
Output:
['name', 'expanse', 'country_code2', 'country_code3'] ['Transitional islamic state of afghanistan', '652090.00', 'AF', 'AFG'] ['Albania', '28748.00', 'AL', 'ALB'] ['Algeria', '2381741.00', 'DZ', 'DZA'] ['American Samoa', '199.00', 'AS', 'ASM'] ...
Code language: Python ( python )
The country.csv
has the first line as the header. To carve up the header and data, yous employ the enumerate()
part to get the index of each line:
import csv with open('country.csv', encoding="utf8") equally f: csv_reader = csv.reader(f) for line_no, line in enumerate(csv_reader, one): if line_no == 1: print('Header:') print(line) # header impress('Data:') else: print(line) # data
Lawmaking language: Python ( python )
In this instance, we use the enumerate()
function and specify the index of the first line as i.
Within the loop, if the line_no is is one, the line is the header. Otherwise, it'south a data line.
Another manner to skip the header is to apply the adjacent()
function. The next()
office forrard to the reader to the adjacent line. For case:
import csv with open('country.csv', encoding="utf8") as f: csv_reader = csv.reader(f) # skip the first row next(csv_reader) # show the data for line in csv_reader: print(line)
Lawmaking linguistic communication: Python ( python )
The following reads the state.csv
file and calculate the total areas of all countries:
import csv total_area = 0 # calculate the total area of all countries with open('state.csv', encoding="utf8") as f: csv_reader = csv.reader(f) # skip the header next(csv_reader) # calculate total for line in csv_reader: total_area += float(line[ane]) impress(total_area)
Code linguistic communication: Python ( python )
Output:
148956306.9
Code language: Python ( python )
Reading a CSV file using the DictReader class
When yous utilize the csv.reader()
part, you can access values of the CSV file using the subclass annotation such as line[0]
, line[one]
, and so on. However, using the csv.reader()
office has two main limitations:
- First, the fashion to access the values from the CSV file is not so obvious. For example, the
line[0]
implicitly ways the country proper name. It would exist more expressive if you tin can access the state name likeline['country_name']
. - Second, when the order of columns from the CSV file is inverse or new columns are added, you need to alter the lawmaking to get the correct data.
This is where the DictReader
class comes into play. The DictReader class likewise comes from the csv
module.
The DictReader
class allows you to create an object similar a regular CSV reader. Only it maps the information of each line to a dictionary (dict
) whose keys are specified by the values of the first line.
By using the DictReader
class, you can access values in the land.csv
file like line['name']
, line['area']
, line['country_code2']
, and line['country_code3']
.
The following example uses the DictReader
class to read the country.csv
file:
import csv with open('country.csv', encoding="utf8") as f: csv_reader = csv.DictReader(f) # skip the header next(csv_reader) # show the information for line in csv_reader: impress(f"The area of {line['name']} is {line['area']} km2")
Code linguistic communication: Python ( python )
Output:
The area of Afghanistan is 652090.00 km2 The area of Albania is 28748.00 km2 The area of Algeria is 2381741.00 km2 ...
Code language: Python ( python )
If yous want to have different field names other than the ones specified in the outset line, you can explicitly specify them past passing a list of field names to the DictReader()
constructor like this:
import csv fieldnames = ['country_name', 'area', 'code2', 'code3'] with open('country.csv', encoding="utf8") every bit f: csv_reader = csv.DictReader(f, fieldnames) next(csv_reader) for line in csv_reader: print(f"The area of {line['country_name']} is {line['area']} km2")
Lawmaking language: Python ( python )
In this case, instead of using values from the start line as the field names, we explicitly pass a list of field names to the DictReader
constructor.
Summary
- Use
csv.reader()
function orcsv.DictReader
class to read information from a CSV file.
Did you detect this tutorial helpful ?
Source: https://www.pythontutorial.net/python-basics/python-read-csv-file/
0 Response to "How to Read Csv in Python as Numbers"
ارسال یک نظر