Skip to main content

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:

Metadata

Release files for countryflag 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for countryflag 1.1.1
File Size Uploaded Provenance Actions
countryflag-1.1.1.tar.gz 78.4 kB Without provenance Details for countryflag-1.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for countryflag 1.1.1
File Interpreter ABI Platform Actions
countryflag-1.1.1-py3-none-any.whl Python 3 none any Details for countryflag-1.1.1-py3-none-any.whl

Total release size: 117.2 kB

Release files / countryflag-1.1.1.tar.gz

Download URL countryflag-1.1.1.tar.gz
Size 78.4 kB
Tags Source
SHA-256 checksum
How to use checksums
dbf968ed08b65d604edf4c82c297949ab57aee80f06dbb81c373b60543100e9c
BLAKE2b-256 checksum
How to use checksums
229042bf753ab2d33778f75e3918e479912e8c6bceb81a811a885445f70d7f7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

Release files / countryflag-1.1.1-py3-none-any.whl

Download URL countryflag-1.1.1-py3-none-any.whl
Size 38.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e69813ac858ab667a6b715b6ed3af2c37216ca22dcf55679d697998e7344663d
BLAKE2b-256 checksum
How to use checksums
f097ded95eeb50ba40bacd8253c76019451248211478706a245538df285f55f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

Security for countryflag 1.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page