In this Python Tutorial we want to talk about Python File Parsing, so Python is powerful programming language and it provides many useful features for developers. one of the best feature in Python is the ability to parse files in different formats. we can say that file parsing is the process of reading and extracting data from a file in structured way. in this Python Tutorial we want to talk about the basics of Python file parsing, we will learn that how to read and manipulate different file formats in Python.
Python File Parsing
First of all let’s talk about the concept of files in Python, in Python a file is a named location on computer file system that stores data. files can be different types like text files, binary files and database files. text files are the most common type of file, and it is used for file parsing. these files store data in human readable format, for example as plain text, comma separated values (CSV) or JSON. let’s look at some examples of how to parse different types of text files using Python.
Parsing TXT File with Python
First of all let’s talk about reading a file in Python, for reading a file in Python we can use builtin Python function open(). this function takes two arguments, file path and the mode in which we want to open the file. mode can be r for reading, w for writing or a for appending.
This is an example of reading a text file in Python, in this example, we want to open a file at name of file.txt, we open the file in read mode. in here with statement ensures that the file is properly closed after we finish the reading. file.read() function reads the entire contents of the file and stores it in the data variable. and lastly we print contents of the file using print() function.
1 2 3 |
with open('file.txt', 'r') as file: data = file.read() print(data) |
We have created a txt file with some texts, and if run the code this will be the result
Parsing CSV File with Python
Now let’s talk about another type of file and that is CSV files, CSV files are commonly used to store tabular data. for parsing a CSV file in Python, we can use builtin csv module.
This is an example of parsing a CSV file using Python, in this example first of all we have imported the csv module. after that we have opened a file at name of file.csv, we open the file in read mode using with statement. after that we creates csv.reader object from the file object, and it allows us to iterate over the rows in the CSV file. and lastly we print each row using the print() function.
1 2 3 4 5 6 |
import csv with open('file.csv', 'r') as file: reader = csv.reader(file) for row in reader: print(row) |
Parsing JSON File with Python
Now it is time to talk about JSON files, JSON files are commonly used to store data in structured format. for parsing a JSON file in Python, we can use builtin json module.
This is an example of parsing a JSON file using Python, in this example first of all we have imported the json module. after that we open a file at name of file.json, we open the file in read mode using with statement. after that we use json.load() function to load the contents of the JSON file into data variable. and lastly we print the contents of the file using print() function.
1 2 3 4 5 |
import json with open('file.json', 'r') as file: data = json.load(file) print(data) |
Subscribe and Get Free Video Courses & Articles in your Email