Member-only story
Here’s a comprehensive list of Python libraries, modules, and tools commonly used for accessing and manipulating various types of documents:
1. Text Documents
Reading and Writing Text Files:
# Reading a text file
with open('example.txt', 'r') as file:
content = file.read()
# Writing to a text file
with open('output.txt', 'w') as file:
file.write('Hello, World!')
Working with CSV Files (Comma-Separated Values):
import csv
# Reading CSV
with open('data.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for row in csv_reader:
print(row)
# Writing to CSV
with open('output.csv', 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['Name', 'Age'])
csv_writer.writerow(['John', 25])
Working with JSON (JavaScript Object Notation):
import json
# Reading JSON
with open('data.json', 'r') as json_file:
data = json.load(json_file)
print(data)
# Writing to JSON
data = {'name': 'John', 'age': 25}
with open('output.json', 'w') as json_file:
json.dump(data, json_file)