Skip to main content

A Python package for converting country names into emoji flags

Project description

CountryFlag

PyPI version Python versions Documentation Status License Code style: black Build Status Coverage

CountryFlag is a Python package for converting country names into emoji flags.

Features

  • Convert country names to emoji flags 🏁
  • Support for reverse lookup (flag to country name)
  • Support for region/continent grouping
  • Multiple output formats (text, JSON, CSV)
  • Fuzzy matching for country names
  • Interactive CLI mode with autocompletion
  • Asynchronous and parallel processing
  • Comprehensive caching system

Installation

pip install countryflag

Quick Start

Python API

import countryflag

# Convert country names to flags
countries = ['Germany', 'BE', 'United States of America', 'Japan']
flags = countryflag.getflag(countries)
print(flags)  # 🇩🇪 🇧🇪 🇺🇸 🇯🇵

# Using the core class
from countryflag.core import CountryFlag

cf = CountryFlag()
flags, pairs = cf.get_flag(["United States", "Canada", "Mexico"])
print(flags)  # 🇺🇸 🇨🇦 🇲🇽

Command Line Interface

# Basic usage - both forms are supported:
countryflag Germany BE Spain 'United States of America'
countryflag --countries Germany BE Spain 'United States of America'

# Custom separator
countryflag --separator "|" Germany France Italy
countryflag --separator "|" --countries Germany France Italy

# Fuzzy matching
countryflag --fuzzy Germny Frnace Itly
countryflag --fuzzy --countries Germny Frnace Itly

# Get European flags
countryflag --region Europe

# Interactive mode
countryflag --interactive

# Output formats
countryflag --format json Germany France
countryflag --format json --countries Germany France
countryflag --format csv Germany France
countryflag --format csv --countries Germany France

# List all supported countries
countryflag --list-countries

# List all supported regions
countryflag --list-regions

# Validate a country name
countryflag --validate "Germany"

# Reverse lookup (flag to country)
countryflag --reverse 🇩🇪 🇫🇷 🇺🇸

# Process files
countryflag --file countries.txt
countryflag --files file1.txt file2.txt

CLI Reference

Available Options

Option Short Description
--countries Country names to convert (space-separated)
--file -i Process a file with country names (one per line)
--files Process multiple files in parallel
--reverse -r Convert flag emojis to country names
--region Get flags for all countries in a region
--interactive -I Run in interactive mode with autocompletion
--format -f Output format: text, json, or csv
--separator -s Character to separate flags (default: space)
--fuzzy -z Enable fuzzy matching for country names
--threshold -t Similarity threshold for fuzzy matching (0-1)
--language -l Language for country names (ISO 639-1 code)
--verbose -v Enable verbose logging
--list-countries List all supported countries
--list-regions List all supported regions
--validate Validate a country name
--cache -c Enable caching
--cache-dir Directory for cache files
--async -a Use asynchronous file processing
--workers -w Number of worker threads for parallel processing

Supported Regions

  • Africa
  • Americas
  • Asia
  • Europe
  • Oceania

Troubleshooting

Common Issues

1. "More than one regular expression match" warnings

These warnings indicate that the country matching algorithm found multiple potential matches. The tool will still work correctly and output the correct flags, but you may see warning messages. This is a known issue that doesn't affect functionality.

2. Country not found

Try using the --fuzzy flag for fuzzy matching:

countryflag --fuzzy --countries "Untied States"  # Will match "United States"

3. Multi-word country names

Always quote multi-word country names:

countryflag --countries "United States of America" "United Kingdom"

4. Check available countries

To see all supported country names:

countryflag --list-countries

Advanced Features

Caching

CountryFlag includes automatic cache sharing for improved performance:

from countryflag.core import CountryFlag
from countryflag.cache import MemoryCache, DiskCache

# Automatic cache sharing (new in v1.0.1, enhanced in v1.1.1)
cf1 = CountryFlag()  # Uses global shared cache
cf2 = CountryFlag()  # Shares same cache as cf1

# First call (cache miss)
flags1, _ = cf1.get_flag(["Germany"])

# Second call from different instance (cache hit!)
flags2, _ = cf2.get_flag(["Germany"])  # Much faster

# Custom cache (if needed)
cache = MemoryCache()
cf = CountryFlag(cache=cache)  # Uses independent cache

# Clear global cache (useful for testing)
CountryFlag.clear_global_cache()

Region-Based Lookup

# Get all European flags
flags, pairs = cf.get_flags_by_region("Europe")
print(f"Found {len(pairs)} European countries")

Reverse Lookup

# Convert flags back to country names
flags = ["🇺🇸", "🇯🇵", "🇩🇪"]
pairs = cf.reverse_lookup(flags)
for flag, country in pairs:
    print(f"{flag} is {country}")

Asynchronous Processing

import asyncio
from countryflag.utils.io import process_file_input_async

async def process_large_file():
    countries = await process_file_input_async("countries.txt")
    cf = CountryFlag()
    flags, pairs = cf.get_flag(countries)
    print(flags)

asyncio.run(process_large_file())

Documentation

For complete documentation, visit countryflag.readthedocs.io.

Contributing

Contributions are welcome! Please read our Contributing Guide for details on how to submit pull requests, report issues, and contribute to the project.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

This package depends on:

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

countryflag-1.1.1.tar.gz (78.4 kB view details)

Uploaded Source

Built Distribution

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

countryflag-1.1.1-py3-none-any.whl (38.8 kB view details)

Uploaded Python 3

File details

Details for the file countryflag-1.1.1.tar.gz.

File metadata

  • Download URL: countryflag-1.1.1.tar.gz
  • Upload date:
  • Size: 78.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for countryflag-1.1.1.tar.gz
Algorithm Hash digest
SHA256 dbf968ed08b65d604edf4c82c297949ab57aee80f06dbb81c373b60543100e9c
MD5 93e3168c99904336061774f5a05755c1
BLAKE2b-256 229042bf753ab2d33778f75e3918e479912e8c6bceb81a811a885445f70d7f7f

See more details on using hashes here.

File details

Details for the file countryflag-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: countryflag-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 38.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.23

File hashes

Hashes for countryflag-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e69813ac858ab667a6b715b6ed3af2c37216ca22dcf55679d697998e7344663d
MD5 d923deb8159f3e97ca3312b59eca0e91
BLAKE2b-256 f097ded95eeb50ba40bacd8253c76019451248211478706a245538df285f55f0

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