9fd0ca9cac
Infra: cache_to_file, cache_to_sqlite, http_download_file, http_get_json, http_post_json, read_file_with_encoding, safe_extract_zip, scan_directory, setup_logger, normalize_zip_filenames. Tipos: 30+ tipos core (agent_action, context, task, message, parse_result...), 6 tipos datascience (entity_candidate, extraction_result...), 2 tipos infra. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1.9 KiB
1.9 KiB
name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, tested, tests, test_file_path, file_path
| name | kind | lang | domain | version | purity | signature | description | tags | uses_functions | uses_types | returns | returns_optional | error_type | imports | tested | tests | test_file_path | file_path | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| read_file_with_encoding | function | py | infra | 1.0.0 | impure | read_file_with_encoding(path: str, encodings: list[str] | None = None) -> str | Lee un archivo de texto intentando multiples encodings en orden hasta encontrar uno que funcione. Util para archivos de origen desconocido (Windows, Latin-1, con BOM, etc.). |
|
false | error_go_core | true |
|
python/functions/infra/read_file_with_encoding_test.py | python/functions/infra/read_file_with_encoding.py |
Ejemplo
# Leer archivo de origen desconocido
content = read_file_with_encoding("/tmp/datos.csv")
# Leer archivo Windows con BOM explicitamente
content = read_file_with_encoding("/tmp/report.txt", encodings=["utf-8-sig", "cp1252"])
Notas
Los encodings por defecto son ["utf-8", "utf-8-sig", "latin-1", "cp1252"]. El orden importa: utf-8 se intenta primero porque es el mas comun. Si el archivo tiene BOM y se quiere que sea eliminado automaticamente, pasar encodings=["utf-8-sig"] o anteponerlo a utf-8 en la lista personalizada.
latin-1 nunca lanza UnicodeDecodeError porque mapea todos los bytes 0x00-0xFF, por lo que actua como fallback universal. Si latin-1 es el ultimo encoding y falla con cp1252 tambien, solo un archivo binario puro (sin mapeo posible) disparara el ValueError.
Raises FileNotFoundError u OSError nativas si el archivo no existe o hay error de I/O — estos no se envuelven en ValueError.