54 lines
2.0 KiB
Python
54 lines
2.0 KiB
Python
|
from Lib.Json import json_decode
|
||
|
from Lib.Yaml import yaml_decode
|
||
|
import csv
|
||
|
import os
|
||
|
|
||
|
|
||
|
class LoadData:
|
||
|
"""
|
||
|
Load the data file from the data directory.
|
||
|
"""
|
||
|
def __init__(self):
|
||
|
home = '%s%s%s' % (os.path.dirname(os.path.dirname(__file__)), os.sep, 'Data')
|
||
|
try:
|
||
|
os.path.exists(home) or os.makedirs(home)
|
||
|
except FileExistsError:
|
||
|
pass
|
||
|
self.home = home
|
||
|
|
||
|
def fromJson(self, filename: str, from_data_home=True):
|
||
|
return json_decode(open('%s/%s' % (self.home, filename) if from_data_home else filename, encoding='utf-8').read())
|
||
|
|
||
|
def fromYaml(self, filename: str, from_data_home=True):
|
||
|
return yaml_decode(open('%s/%s' % (self.home, filename) if from_data_home else filename, encoding='utf-8').read())
|
||
|
|
||
|
def fromCsv(self, filename: str, from_data_home=True):
|
||
|
"""
|
||
|
Load data from CSV file, compatible standard format.
|
||
|
:param filename: Filename or path.
|
||
|
:param from_data_home: Whether to load from the data directory, otherwise use the absolute path.
|
||
|
:return: List.
|
||
|
"""
|
||
|
file = '%s/%s' % (self.home, filename) if from_data_home else filename
|
||
|
try:
|
||
|
open(file).read(4096)
|
||
|
encoding = None
|
||
|
except UnicodeDecodeError:
|
||
|
encoding = 'utf-8'
|
||
|
return [row for row in csv.reader(open(file, encoding=encoding))]
|
||
|
|
||
|
def fromTxt(self, filename: str, from_data_home=True):
|
||
|
"""
|
||
|
Load the data from the txt text file, and one line is used as a data.
|
||
|
:param filename: Filename or path.
|
||
|
:param from_data_home: Whether to load from the data directory, otherwise use the absolute path.
|
||
|
:return: List.
|
||
|
"""
|
||
|
file = '%s/%s' % (self.home, filename) if from_data_home else filename
|
||
|
try:
|
||
|
open(file).read(4096)
|
||
|
encoding = None
|
||
|
except UnicodeDecodeError:
|
||
|
encoding = 'utf-8'
|
||
|
return [row.rstrip("\n") for row in open(file, encoding=encoding).readlines()]
|