Skip to main content

File format handler packages for JSON, CSV, XML, and Excel files

Project description

echoss_fileformat v1.2.1

File Format Handlers

This project provides file format handler packages for JSON, CSV, XML, and Excel files. The packages provide an abstraction layer to load and save data in these file formats using a unified API.

Version History

  • v1.0 : Object method
  • v1.1 : static method
  • v1.2 : stable updated
  • v1.2.7 : to_table() method minor update and debug
  • v1.2.9 : misspelling correct

Installation

To install the package, use pip: pip install echoss_fileformat

To upgrade the installed package, use pip: pip install echoss_fileformat -U

Usage

  1. static call
from echoss_fileformat import FileUtil, to_table, get_logger, set_logger_level

# FileUtil Load test_data from a file
csv_df = FileUtil.load('test_data.csv')
excel_df = FileUtil.load('weather_data.xls')
json_df = FileUtil.load('test_data.json', data_key = 'data')
jsonl_df = FileUtil.load('test_multiline_json.jsonl', data_key = 'data')

# FileUtil Save DataFrame to a file
FileUtil.dump(excel_df, 'save/test_data_1.csv')
FileUtil.dump(csv_df, 'weather_data.xlsx')
FileUtil.dump(json_df, 'test_data_2.jsonl')

# read/write config file
app_config = FileUtil.dict_load('config/application.yaml')
db_config = FileUtil.dict_load('jdbc.properties')
FileUtil.dict_dump(app_config, 'new_config.xml')

# DataFrame to table-like string
logger = get_logger("echoss_fileformat_test")
set_logger_level("DEBUG")
logger.debug(to_table(json_df))

파일 포맷 읽기 :

  • FileUtil.load(file_path: str, file_format=None, **kwargs) : 파일 확장자 기준으로 매칭되는 파일포맷으로 읽음
    • 확장자 .csv : 기본csv 파일 포맷으로 읽기. 컬럼 구분자는 콤마(,) 사용.
    • 확장자 .tsv : 컬럼 구분자 탭(\t)인 csv 파일 포맷으로 읽기.
    • 확장자 .xls .xlsx : excel 파일 포맷으로 읽기
    • 확장자 .json : 파일 전체가 1개의 json 객체인 파일포맷으로읽기 (전체가 JSON array 인경우는 객체로 감싸야함)
    • 확자자 .jsonl : 라인 하나가 json 객체인 multi line json 파일 형태
    • 확장자 .xml : xml 파일포맷으로 읽기
    • 확장자 .parquet : parquet 파일포맷으로 읽기
    • 확장자 .feather : feather 파일포맷으로 읽기
  • FileUtil.load(filename_or_file, file_format='csv') : csv 파일 포맷으로 파일명 또는 file-like object 로 읽음
  • FileUtil.load(filename_or_file, file_format='xlsx') : excel 파일 포맷으로 파일명 또는 file-like object 로 읽음
  • FileUtil.load(filename_or_file, file_format='json') : json 파일 포맷으로 파일명 또는 file-like object 로 읽음

파일 포맷 쓰기 :

  • FileUtil.dump(df: pd.DataFrame, file_path: str, file_format=None, force_write=False, **kwargs) : 파일 확장자 기준으로 매칭되는 파일포맷으로 쓰기
    • 확장자 .csv : 기본csv 파일 포맷으로 쓰기. 컬럼 구분자는 콤마(,) 사용.
    • 확장자 .tsv : 컬럼 구분자 탭(\t)인 csv 파일 포맷으로 쓰기.
    • 확장자 .xls .xlsx : excel 파일 포맷으로 쓰기
    • 확장자 .json : 파일 전체가 1개의 json 객체인 파일포맷으로쓰기 (전체가 JSON array 인경우는 객체로 감싸야함)
    • 확자자 .jsonl : 라인 하나가 json 객체인 multi line json 파일 형태
    • 확장자 .xml : xml 파일포맷으로 쓰기
    • 확장자 .parquet : parquet 파일포맷으로 쓰기
    • 확장자 .feather : feather 파일포맷으로 쓰기
  • FileUtil.dump(filename_or_file, file_format='jsonl') : jsonl 파일 포맷으로 파일명 또는 file-like object 로 쓰기
  • FileUtil.dump_xml(filename_or_file, file_format='xml', force_write=True) : xml 파일 포맷으로 overwrite 파일 쓰기

For config file load/dump:

dict_load() : load file format to dictionary for configuration files .yaml, .xml, .json and .properties dict_dump() : write dictionary to file format .yaml, .xml, .json and .properties

For print dataframe:

def to_table(df: pd.DataFrame, index=True, max_cols=16, max_rows=10, col_space=4, max_colwidth=24, fmt='grid'): dataframe to table-like string

exampe:

+--------------------------+----------------+----------------+---------------+--------------------+------------------+-----------------+-----------------+
| ('수집항목', '개체번호') | ('과폭', 'cm') | ('과고', 'cm') | ('과중', 'g') | ('당도', 'Brix %') | ('산도', '0-14') | ('경도', 'kgf') | ('수분율', '%') |
+--------------------------+----------------+----------------+---------------+--------------------+------------------+-----------------+-----------------+
| 1                        | 8.2            | 6.6            | 253           | 3.1                | 4.0              | 2.71            | 71.2            |
+--------------------------+----------------+----------------+---------------+--------------------+------------------+-----------------+-----------------+
| 2                        | 6.3            | 5.7            | 136           | 3.1                | 4.0              | 2.74            | 72.2            |
+--------------------------+----------------+----------------+---------------+--------------------+------------------+-----------------+-----------------+
| 3                        | 8.0            | 6.3            | 220           | 3.4                | 4.5              | 2.71            | 70.4            |
+--------------------------+----------------+----------------+---------------+--------------------+------------------+-----------------+-----------------+
| 4                        | 7.3            | 6.0            | 173           | 3.7                | 4.0              | 2.75            | 73.78           |
+--------------------------+----------------+----------------+---------------+--------------------+------------------+-----------------+-----------------+
| 5                        | 6.3            | 5.5            | 130           | 3.6                | 4.0              | 2.72            | 75.4            |
+--------------------------+----------------+----------------+---------------+--------------------+------------------+-----------------+-----------------+
  1. Object
  • 학습데이터가 아닌 메타데이터 객체로 읽어들일 경우

handler = CsvHandler('object')

  • 학습데이터로 읽어들이는 경우

handler = ExcelHandler() 또는 handler = ExcelHandler('array')

  • JSON 파일 중에서 각 줄이 하나의 json 객체일 경우

handler = JsonHandler('multiline')

The package provides an abstraction layer to load and save data in JSON, CSV, XML, and Excel formats. The API includes the following methods:

  • load(file_or_filename, **kwargs): Load data from a file.
  • loads(bytes_or_str, **kwargs): Load data from a string.
  • dump(file_or_filename, data = None, **kwargs): Save data to a file.
  • dumps(data = None, **kwargs): Save data to a string.

The following example demonstrates how to load data from a CSV file and save it as a JSON file:

from echoss_fileformat import CsvHandler, JsonHandler

# Load test_data from a CSV file
csv_handler = CsvHandler()
data = csv_handler.load('test_data.csv', header=[0, 1])

# Save test_data as a JSON file
json_handler = JsonHandler('array')
json_handler.load( 'test_data_1.json', data_key = 'data')
json_handler.load( 'test_data_2.json', data_key = 'data')
json_handler.dump( 'test_data_all.json')

Contributing

Contributions are welcome! If you find a bug or want to suggest a new feature, please open an issue on the GitHub repository.

License

This project is licensed under the LGPL License. See the LICENSE file for more information.

Credits

This project was created by 12cm. Special thanks to 12cm R&D for their contributions to the project.

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

echoss_fileformat-1.3.2.tar.gz (225.3 kB view details)

Uploaded Source

Built Distribution

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

echoss_fileformat-1.3.2-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

Details for the file echoss_fileformat-1.3.2.tar.gz.

File metadata

  • Download URL: echoss_fileformat-1.3.2.tar.gz
  • Upload date:
  • Size: 225.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for echoss_fileformat-1.3.2.tar.gz
Algorithm Hash digest
SHA256 92bf974febc4722df7b8636b91aa1f5a83be5a6509e04ab79c2ebf7c18d73af8
MD5 5fae97588f63551fd069e82bc5486d35
BLAKE2b-256 6a1ec8a50df13275147194beecf0f07f22381a4a1963f19a09d7de409a76b682

See more details on using hashes here.

File details

Details for the file echoss_fileformat-1.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for echoss_fileformat-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 89a889ca9344f3f37d07c41a2b6ee2eaef1445102e7a01341e80f1e4d2ef490a
MD5 30f4b0cf3b0325fdf657bc0f227c50b4
BLAKE2b-256 2af7d6de364d07dde8fc3280c9afa898796d65799d08c1734a11cf2b60bd7180

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