Skip to main content

A simple utility to extract specific fields from CSV files

Project description

CSV Field Extractor

GitHub release Python 3.6+ License: MIT GitHub issues GitHub stars Code style: flake8 Dependencies

A simple Python utility to extract specific fields/columns from CSV files.

Features

  • Extract any field by name from a CSV file
  • Optional alphabetical sorting of results
  • Automatic filtering of empty/null values
  • Field validation with helpful error messages
  • Clean, stripped values (removes whitespace)

Requirements

  • Python 3.x
  • pandas library

Install pandas if you don't have it:

pip install pandas

Usage

Command Line Interface (Recommended for local use)

# Basic usage - extract and print field values line by line
python csv_field_extractor.py Results.csv Symbol

# Sort alphabetically
python csv_field_extractor.py Results.csv Symbol --sort

# Different output formats
python csv_field_extractor.py Results.csv Symbol --sort --output comma
python csv_field_extractor.py Results.csv Description --output space

# Get help
python csv_field_extractor.py --help

As a Python Library

from csv_field_extractor import extract_field_from_csv

# Extract symbols from a CSV file
symbols = extract_field_from_csv('Results.csv', 'Symbol')
print(symbols)

Library Usage Examples

# Extract and sort alphabetically
symbols = extract_field_from_csv('Results.csv', 'Symbol', sort_alphabetically=True)
for symbol in symbols:
    print(symbol)
# Extract descriptions
descriptions = extract_field_from_csv('Results.csv', 'Description')

# Extract expense ratios
expense_ratios = extract_field_from_csv('Results.csv', 'Net Expense Ratio')

# Extract any field
morningstar_ratings = extract_field_from_csv('Results.csv', 'Morningstar Overall')

Command Line Options

  • csv_file: Path to your CSV file (required)
  • field_name: Name of the column/field to extract (required)
  • --sort, -s: Sort results alphabetically
  • --output, -o: Output format - lines (default), comma, or space

Examples

# Extract symbols, sorted, comma-separated (perfect for copy-paste)
python csv_field_extractor.py Results.csv Symbol --sort --output comma

# Extract descriptions as separate lines
python csv_field_extractor.py Results.csv Description

# Quick field validation (see available fields if you mistype)
python csv_field_extractor.py Results.csv WrongFieldName

Function Parameters

  • csv_file_path (str): Path to your CSV file
  • field_name (str): Name of the column/field to extract
  • sort_alphabetically (bool, optional): Sort results alphabetically (default: False)

Return Value

Returns a list of strings containing the field values, with empty/null values filtered out.

Error Handling

If the specified field name doesn't exist in the CSV, the function will raise a ValueError with a list of available field names to help you identify the correct field name.

Example Output

symbols = extract_field_from_csv('Results.csv', 'Symbol', sort_alphabetically=True)
# Returns: ['ARKF', 'ARKG', 'ARKK', 'ARKQ', 'ARKW', 'ARKX', 'IZRL', 'PRNT']

Development

Setup Development Environment

# Clone the repository
git clone https://github.com/yourusername/csv-field-extractor.git
cd csv-field-extractor

# Create virtual environment and install dependencies
make setup

# Or manually:
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
pip install -r requirements.txt

Running Tests

# Run tests using make
make test

# Or run directly
python -m unittest test_csv_field_extractor.py -v

# Run specific test class
python -m unittest test_csv_field_extractor.TestCSVFieldExtractor -v

Installing as Package

# Install in development mode (changes reflected immediately)
pip install -e .

# Now you can use the command anywhere
csv-extract /path/to/data.csv Symbol --sort

Other Make Commands

make setup    # Set up virtual environment
make test     # Run tests
make clean    # Remove build artifacts and cache files
make lint     # Check code style (requires: pip install flake8)
make build    # Build distribution packages
make help     # Show all available commands

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature-name)
  3. Make your changes and add tests
  4. Run tests (make test)
  5. Commit your changes (git commit -am 'Add feature')
  6. Push to the branch (git push origin feature-name)
  7. Create a Pull Request

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

csv_field_extractor-1.0.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

csv_field_extractor-1.0.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file csv_field_extractor-1.0.0.tar.gz.

File metadata

  • Download URL: csv_field_extractor-1.0.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for csv_field_extractor-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8c3528a8a6ce8afa618409e52140644be5739976e854981eecd84c8ed1b842fb
MD5 522aac2ea40f2e283a64ce33411c5764
BLAKE2b-256 ff90916df557fffdc4b4a808427f436261d756f9f21ac8a3be81c1e1d5eb65d8

See more details on using hashes here.

File details

Details for the file csv_field_extractor-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for csv_field_extractor-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd32d9478cb511658ac43acd8ec9c91af4b1048436474419fa25e8ded58a9e11
MD5 7f54821ab554e3ef38587d24ebf5be0e
BLAKE2b-256 0db2141d86f0d257228342000d313b7c4790619607e1d92d601d437c535a5111

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page