-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
โจ Feat: Write CSV, TSV, JSON (close: #69)
- Loading branch information
Showing
3 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from zerohertzLib.util.data import MakeData | ||
from zerohertzLib.util.json import Json, JsonDir | ||
from zerohertzLib.util.write import write_csv, write_json | ||
|
||
__all__ = ["Json", "JsonDir", "MakeData"] | ||
__all__ = ["Json", "JsonDir", "MakeData", "write_csv", "write_json"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import json | ||
import os | ||
from typing import Any, Dict, List, Optional, Union | ||
|
||
|
||
def write_csv(data: List[List[Any]], path: str, tsv: Optional[bool] = False) -> str: | ||
"""CSV (Comma-Separated Values) ํน์ TSV (Tab-Separated Values)๋ฅผ ์์ฑํ๋ ํจ์ | ||
Args: | ||
data (``List[List[Any]]``): ์ ๋ ฅ ๋ฐ์ดํฐ (header ํฌํจ ๋ฌด๊ด) | ||
path (``str``): ์ถ๋ ฅ๋ CSV ํน์ TSV ๊ฒฝ๋ก ๋ฐ ํ์ผ๋ช | ||
tsv (``Optional[bool]``): TSV ์์ฑ ์ฌ๋ถ | ||
Returns: | ||
``str``: ํ์ผ์ ์ ๋ ๊ฒฝ๋ก | ||
Examples: | ||
>>> zz.util.write_csv([["id", "์ข ์กฑ", "์ ์"], ["5hi9", "ํ๋กํ ์ค", 1248], ["gor2", "ํ ๋", 2309], ["gk03", "์ ๊ทธ", 291]], "zerohertzLib/star_craft") | ||
'/.../star_craft.csv' | ||
>>> zz.util.write_csv([["id", "์ข ์กฑ", "์ ์"], ["5hi9", "ํ๋กํ ์ค", 1248], ["gor2", "ํ ๋", 2309], ["gk03", "์ ๊ทธ", 291]], "zerohertzLib/star_craft", True) | ||
'/.../star_craft.tsv' | ||
""" | ||
if tsv: | ||
with open(f"{path}.tsv", "w", encoding="utf-8") as f: | ||
for d in data: | ||
f.writelines("\t".join(list(map(str, d))) + "\n") | ||
return os.path.abspath(f"{path}.tsv") | ||
else: | ||
with open(f"{path}.csv", "w", encoding="utf-8") as f: | ||
for d in data: | ||
f.writelines(",".join(list(map(str, d))) + "\n") | ||
return os.path.abspath(f"{path}.csv") | ||
|
||
|
||
def write_json(data: Union[Dict[Any, Any], List[Dict[Any, Any]]], path: str) -> str: | ||
"""JSON (JavaScript Object Notation)๋ฅผ ์์ฑํ๋ ํจ์ | ||
Args: | ||
data (``Dict[Any, Any]``): ์ ๋ ฅ ๋ฐ์ดํฐ (header ํฌํจ ๋ฌด๊ด) | ||
path (``str``): ์ถ๋ ฅ๋ json ํ์ผ์ ๊ฒฝ๋ก ๋ฐ ํ์ผ๋ช | ||
Returns: | ||
``str``: ํ์ผ์ ์ ๋ ๊ฒฝ๋ก | ||
Examples: | ||
>>> zz.util.write_json([{"id": "4169", "์ ํฌ๋ ฅ": 4209, "์ ๋ณด": ["์๋ฌด", "๊ฑฐ๋"]}]*100, "zerohertzLib/star_craft") | ||
'/.../star_craft.json' | ||
[ | ||
{ | ||
"id": "4169", | ||
"์ ํฌ๋ ฅ": 4209, | ||
"์ ๋ณด": [ | ||
"์๋ฌด", | ||
"๊ฑฐ๋" | ||
] | ||
}, | ||
{ | ||
"id": "4169", | ||
"์ ํฌ๋ ฅ": 4209, | ||
"์ ๋ณด": [ | ||
"์๋ฌด", | ||
"๊ฑฐ๋" | ||
] | ||
}, | ||
... | ||
""" | ||
with open(f"{path}.json", "w", encoding="utf-8") as f: | ||
json.dump(data, f, indent=4, ensure_ascii=False) | ||
return os.path.abspath(f"{path}.json") |