Skip to content

Commit

Permalink
โœจ Feat: Write CSV, TSV, JSON (close: #69)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerohertz committed Nov 17, 2023
1 parent 85e3220 commit 55c00d7
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
35 changes: 35 additions & 0 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,38 @@ def test_JsonDir():
assert jsd._getKey("language") == "head/repo/language"
assert type(jsd.unique("sha")) == set
jsd.tree()


def test_write_csv():
zz.util.write_csv(
[
["id", "์ข…์กฑ", "์ ์ˆ˜"],
["5hi9", "ํ”„๋กœํ† ์Šค", 1248],
["gor2", "ํ…Œ๋ž€", 2309],
["gk03", "์ €๊ทธ", 291],
],
"star_craft",
)
assert "star_craft.csv" in os.listdir()


def test_write_csv():
zz.util.write_tsv(
[
["id", "์ข…์กฑ", "์ ์ˆ˜"],
["5hi9", "ํ”„๋กœํ† ์Šค", 1248],
["gor2", "ํ…Œ๋ž€", 2309],
["gk03", "์ €๊ทธ", 291],
],
"star_craft",
True,
)
assert "star_craft.tsv" in os.listdir()


def test_write_json():
zz.util.write_json(
[{"id": "4169", "์ „ํˆฌ๋ ฅ": 4209, "์ •๋ณด": ["์•„๋ฌด", "๊ฑฐ๋‚˜"]}] * 100,
"star_craft",
)
assert "star_craft.json" in os.listdir()
3 changes: 2 additions & 1 deletion zerohertzLib/util/__init__.py
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"]
69 changes: 69 additions & 0 deletions zerohertzLib/util/write.py
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")

0 comments on commit 55c00d7

Please sign in to comment.