๐Ÿš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more โ†’
Socket
Book a DemoInstallSign in
Socket

echoss-fileformat

Package Overview
Dependencies
Maintainers
0
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

echoss-fileformat

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

1.2.2
PyPI
Maintainers
0

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

Installation

To install the package, use pip: pip install echoss_fileformat

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

Usage

  • 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): 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            |
+--------------------------+----------------+----------------+---------------+--------------------+------------------+-----------------+-----------------+
  • 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.

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts