In modern software development, working with different file formats is a routine task. Python offers extensive and easy-to-use libraries to handle common file formats such as text files, CSV files, and JSON files. Understanding how to read, write, and manipulate these files is essential for any Python developer. This guide provides a comprehensive walkthrough of how to work with each of these formats effectively.
File formats determine how data is stored in a file. Text files store plain text, CSV files are used for tabular data, and JSON files store structured data in a readable format. Each format has its own characteristics and best use cases. Python's built-in modules like open, csv, and json make it easy to interact with these file types.
A text file (.txt) contains sequences of characters and is one of the simplest forms of storing data. It can be used for storing notes, logs, configuration files, or any other human-readable content.
To read a text file in Python, you can use the open() function with mode 'r':
with open('example.txt', 'r') as file:
content = file.read()
print(content)
Sometimes it's useful to read a file line by line:
with open('example.txt', 'r') as file:
for line in file:
print(line.strip())
Use mode 'w' to write to a file. This will overwrite existing content:
with open('example.txt', 'w') as file:
file.write("Hello, World!")
Use mode 'a' to add new content at the end of an existing file:
with open('example.txt', 'a') as file:
file.write("\nAnother line.")
Use mode 'r+' or 'w+' to both read and write:
with open('example.txt', 'r+') as file:
content = file.read()
file.write("\nAppended after reading.")
CSV (Comma Separated Values) is a popular file format used for storing tabular data. Each row corresponds to a record, and each value is separated by a comma. Python provides a built-in csv module to handle CSV files easily.
You can read a CSV file using csv.reader():
import csv
with open('data.csv', newline='') as file:
reader = csv.reader(file)
for row in reader:
print(row)
This reads each line as a list of strings.
Use csv.DictReader() to read each row as a dictionary:
import csv
with open('data.csv', newline='') as file:
reader = csv.DictReader(file)
for row in reader:
print(row['name'], row['age'])
Use csv.writer() to write rows to a CSV file:
import csv
data = [['name', 'age'], ['Alice', 30], ['Bob', 25]]
with open('data.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)
import csv
data = [{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]
with open('data.csv', 'w', newline='') as file:
fieldnames = ['name', 'age']
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data)
CSV files are not always comma-separated. You can specify a different delimiter:
import csv
with open('data.tsv', newline='') as file:
reader = csv.reader(file, delimiter='\t')
for row in reader:
print(row)
Use try-except blocks to catch csv.Error when dealing with malformed CSV files.
try:
with open('corrupt.csv') as file:
reader = csv.reader(file)
for row in reader:
print(row)
except csv.Error as e:
print("CSV Error:", e)
JSON (JavaScript Object Notation) is a format used for storing structured data. It is widely used in web APIs and configuration files. Python provides a built-in json module for working with JSON data.
Use json.load() to read a JSON file:
import json
with open('data.json', 'r') as file:
data = json.load(file)
print(data)
import json
data = {'name': 'Alice', 'age': 30, 'skills': ['Python', 'Data Science']}
with open('data.json', 'w') as file:
json.dump(data, file)
You can use the indent parameter to format the output:
json.dump(data, file, indent=4)
Use json.loads() and json.dumps() to convert between strings and Python objects:
json_str = '{"name": "Bob", "age": 25}'
data = json.loads(json_str)
print(data)
string = json.dumps(data)
print(string)
Use try-except to catch json.JSONDecodeError:
try:
with open('bad.json') as file:
data = json.load(file)
except json.JSONDecodeError:
print("Invalid JSON format")
| Feature | Text | CSV | JSON |
|---|---|---|---|
| Structure | Plain text | Tabular | Key-value (nested supported) |
| Best for | Logs, simple notes | Spreadsheet-like data | Hierarchical or API data |
| Parsing Complexity | Low | Medium | High |
| Supported Libraries | open() | csv | json |
| Human Readable | Yes | Yes | Yes |
Use text files to store logs. Easy to write and read using write() and readlines().
CSV files are commonly used for dataset exchange between Excel and data analysis tools.
JSON files are widely used in RESTful APIs and for storing application configuration settings.
Always specify encoding explicitly for non-ASCII text:
with open('unicode.txt', 'r', encoding='utf-8') as file:
data = file.read()
import os
if not os.path.exists("file.txt"):
with open("file.txt", "w") as file:
file.write("Created!")
import pandas as pd
# CSV
df = pd.read_csv("data.csv")
df.to_csv("out.csv", index=False)
# JSON
df = pd.read_json("data.json")
df.to_json("out.json")
Python provides rich capabilities for working with different file formats such as Text, CSV, and JSON. Each of these formats serves a unique purpose and is suited to particular types of data storage and manipulation. Text files are great for logs and notes; CSV is ideal for spreadsheets and tabular data; JSON is perfect for structured, hierarchical data commonly used in APIs and configs.
By mastering the file handling techniques in Python for these formats, you enhance your ability to build data-driven applications, automate data processing workflows, and interact with external data sources efficiently.
Python is commonly used for developing websites and software, task automation, data analysis, and data visualisation. Since it's relatively easy to learn, Python has been adopted by many non-programmers, such as accountants and scientists, for a variety of everyday tasks, like organising finances.
Learning Curve: Python is generally considered easier to learn for beginners due to its simplicity, while Java is more complex but provides a deeper understanding of how programming works.
The point is that Java is more complicated to learn than Python. It doesn't matter the order. You will have to do some things in Java that you don't in Python. The general programming skills you learn from using either language will transfer to another.
Read on for tips on how to maximize your learning. In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes. Developing mastery of Python's vast array of libraries can take months or years.
6 Top Tips for Learning Python
The following is a step-by-step guide for beginners interested in learning Python using Windows.
Best YouTube Channels to Learn Python
Write your first Python programStart by writing a simple Python program, such as a classic "Hello, World!" script. This process will help you understand the syntax and structure of Python code.
The average salary for Python Developer is βΉ5,55,000 per year in the India. The average additional cash compensation for a Python Developer is within a range from βΉ3,000 - βΉ1,20,000.
Copyrights © 2024 letsupdateskills All rights reserved