A simple utility to extract specific fields from CSV files
Project description
CSV Field Extractor
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, orspace
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
- Fork the repository
- Create a feature branch (
git checkout -b feature-name) - Make your changes and add tests
- Run tests (
make test) - Commit your changes (
git commit -am 'Add feature') - Push to the branch (
git push origin feature-name) - Create a Pull Request
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file csv_field_extractor-1.0.1.tar.gz.
File metadata
- Download URL: csv_field_extractor-1.0.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bc793e82dd38d9b58b5caecf2826733bffeec295c42aaa969d5d0891abbb9b6
|
|
| MD5 |
1a34dcc739ec312fb9708e93cd33688d
|
|
| BLAKE2b-256 |
3d17183e6dc7d647a7b599d581ffaa1ef9d086e15e54a3d47bb65f09c1815747
|
File details
Details for the file csv_field_extractor-1.0.1-py3-none-any.whl.
File metadata
- Download URL: csv_field_extractor-1.0.1-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f60419695b77effde0c4751b95664b08e8e2b670802e4d0c93bcb20912d17004
|
|
| MD5 |
8c43655d4b54ed338e193ecc1a97f3c0
|
|
| BLAKE2b-256 |
54a54a890e3f30a66494c0fcc5e7722d1a321fc61d86b622e663a450a6191141
|