Read CSV File Line by Line in Python (Example)
In this tutorial, I’ll demonstrate how to import a CSV file row by row in Python.
The article consists of this content:
It’s time to dive into the example:
Exemplifying Data & Libraries
First, we have to import the pandas library:
import pandas as pd # Import pandas library to Python |
import pandas as pd # Import pandas library to Python
Next, we also need to create some example data:
data = pd.DataFrame({'x1':range(1, 7), # Create pandas DataFrame 'x2':['a', 'b', 'c', 'd', 'e', 'f'], 'x3':range(57, 51, - 1), 'x4':[5, 1, 8, 8, 4, 2]}) print(data) # Print pandas DataFrame |
data = pd.DataFrame({'x1':range(1, 7), # Create pandas DataFrame 'x2':['a', 'b', 'c', 'd', 'e', 'f'], 'x3':range(57, 51, - 1), 'x4':[5, 1, 8, 8, 4, 2]}) print(data) # Print pandas DataFrame
Table 1 illustrates the structure of our example data – It has six data points and four columns.
We can now save this data set in a CSV file as shown below:
data.to_csv('data.csv', index = False) # Export pandas DataFrame to CSV |
data.to_csv('data.csv', index = False) # Export pandas DataFrame to CSV
After executing the previous syntax, a new CSV file has been created in the current working directory. This CSV file will be used as a basement for the example that I’m showing next.
Example: Load pandas DataFrame in CSV File Line by Line
The following code explains how to import a CSV file row by row.
For this task, we first need to load the csv library, in order to use the functions that are contained in the library:
import csv # Import csv |
import csv # Import csv
Next, we can use the functions of the csv library in a for loop to print each line of our CSV file separately to the Python console:
with open('data.csv', 'r') as f: # Read lines separately reader = csv.reader(f, delimiter='t') for i, line in enumerate(reader): print(i, line) # 0 ['x1,x2,x3,x4'] # 1 ['1,a,57,5'] # 2 ['2,b,56,1'] # 3 ['3,c,55,8'] # 4 ['4,d,54,8'] # 5 ['5,e,53,4'] # 6 ['6,f,52,2'] |
with open('data.csv', 'r') as f: # Read lines separately reader = csv.reader(f, delimiter='t') for i, line in enumerate(reader): print(i, line) # 0 ['x1,x2,x3,x4'] # 1 ['1,a,57,5'] # 2 ['2,b,56,1'] # 3 ['3,c,55,8'] # 4 ['4,d,54,8'] # 5 ['5,e,53,4'] # 6 ['6,f,52,2']
The previous output shows each line of our data set. You may now adjust the previous code according to your own needs.
Video, Further Resources & Summary
In case you require more explanations on the Python codes of the present post, I recommend watching the following video on the Statistics Globe YouTube channel. In the video, I’m explaining the Python syntax of this article.
The YouTube video will be added soon.
Furthermore, you may have a look at the related tutorials on https://www.statisticsglobe.com/. I have published several articles already:
- Basic Course for the pandas Library in Python
- Read CSV File as pandas DataFrame in Python
- Skip Rows but Keep Header when Reading CSV File
- Skip First Row when Reading pandas DataFrame from CSV File
- Ignore Header when Reading CSV File as pandas DataFrame
- Read Only Certain Columns of CSV File as pandas DataFrame in Python
- Introduction to Python Programming
This article has explained how to read a pandas DataFrame in a CSV file line by line in the Python programming language. In case you have additional questions and/or comments, let me know in the comments section.