KBlueLeaf's picture
Upload write_csv.py
85763d3 verified
raw
history blame
1.23 kB
from tqdm import tqdm
from db import *
from playhouse.shortcuts import model_to_dict
def make_csv_header():
post = Post.get_by_id(1)
return ",".join([field for field in model_to_dict(post).keys()])
rating_enum = {
"general": 0,
"sensitive": 1,
"questionable": 2,
"explicit": 3,
}
def read_rows():
for data in tqdm(db.execute_sql("SELECT * FROM post;").fetchall()):
data = [
(
(" ".join(str(int(tag, 36)) for tag in i[1:-1].split("#$")))
if isinstance(i, str) and i and i[0] == "$" and i[-1] == "#"
else int(i)
if isinstance(i, bool)
else ""
if i is None or i == ""
else rating_enum[i]
if i in rating_enum
else '"{}"'.format(i.replace('"', '""'))
if "," in str(i)
else i
)
for i in data
]
assert len(data) == 38, ",".join([str(i) for i in data])
csv_str = ",".join([str(i) for i in data])
yield csv_str
with open("./db.csv", "w", encoding="utf-8") as f:
f.write(f"{make_csv_header()}\n")
f.writelines(f"{i}\n" for i in read_rows())