Skip to main content

Files in AI projects

When working with Python for AI, you’ll constantly work with data files. Your data might come as:
  • CSV files - Spreadsheet data from Excel or databases
  • JSON files - API responses and configuration data
  • XML files - Structured data from various systems
  • Text files - Raw text for processing
  • Parquet files - Efficient data storage format
The good news? Python has excellent libraries for all of these.

Common libraries for files

Each file type has specialized libraries: CSV files:
  • pandas - Best for data analysis (recommended)
  • csv module - Built-in, for simple operations
JSON files:
  • json module - Built-in, handles all JSON operations
  • pandas - Can read/write JSON with DataFrames
Other formats:
  • xml.etree - Built-in XML parsing
  • openpyxl - Excel files (.xlsx)
  • PyPDF2 - PDF files

Working with our sales data

Let’s work with our CSV file and convert it to different formats. First, install pandas:
If you get an error, try pip3 install pandas or install it through VS Code’s terminal.
Update your analyzer.py:

File format comparison

Different formats have different uses:

Loading different file types

Here’s how to load various formats:

Learn more

To dive deeper into file handling:

Organizing code

Split your code into reusable functions