Data Handling in Python – Reading and Writing Files and Parsing Data from Various Sources

Data handling is a fundamental topic in any programming language, and Python is particularly flexible and allows for quick and easy data processing from various sources.
Reading and Writing Data to/from Files
In Python, there are several ways to read data from a file. The simplest method is to use the open()
function, which allows you to open a file for reading or writing. You can then read the file’s contents using the read()
or readlines()
method. The following example illustrates how to read the entire contents of a file into a variable:
with open('file.txt', 'r') as f:
contents = f.read()
If you want to read the file line by line, you can use a for
loop:
with open('file.txt', 'r') as f:
for line in f:
print(line)
Writing data to a file is just as easy. You can use the same open()
function, but this time open the file for writing using the 'w'
parameter or for appending to an existing file using the 'a'
parameter. The following example illustrates how to save data to a file:
with open('file.txt', 'w') as f:
f.write('This is the new contents of the file')
Parsing and Processing Data from Various Sources
Sometimes the data we want to process is stored in a special format, such as a CSV (Comma Separated Values) file or a JSON (JavaScript Object Notation) file. In these cases, we can use the appropriate libraries that allow for quick and easy parsing of such data.
CSV files are text files in which the individual data fields are separated by commas. We can read such a file using the csv
module:
import csv
with open('data.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
JSON files are text files stored in a format similar to JavaScript objects. We can read such a file using the json.load()
function from the json
module:
import json
with open('data.json', 'r') as f:
data = json.load(f)
print(data)
In addition to reading data from files, Python also allows for easy processing of data from the web. For example, we can read data from an API (Application Programming Interface) using the requests
module. The following example illustrates how to read data from an API using the requests
module and save it to a JSON file:
import requests
import json
URL = 'https://api.example.com/data'
response = requests.get(URL)
data = response.json()
with open('data.json', 'w') as f:
json.dump(data, f)
Text generated using chat.openai.com.