(filename, file_type=None)
| 27 | |
| 28 | |
| 29 | def load_data(filename, file_type=None): |
| 30 | if file_type is None: |
| 31 | file_type = ['json'] |
| 32 | if filename.endswith('.jsonl'): |
| 33 | return [json.loads(line) for line in open(filename)] |
| 34 | elif filename.endswith('.json'): |
| 35 | return json.load(open(filename, 'r')) |
| 36 | elif os.path.isdir(filename): |
| 37 | files = [os.path.join(filename, f) for f in os.listdir(filename) if any(f.endswith(ext) for ext in file_type)] |
| 38 | return [load_data(f) for f in files] |
| 39 | elif filename.endswith('.txt'): |
| 40 | with open(filename, 'r') as f: |
| 41 | data = [line.strip() for line in f] |
| 42 | return data |
| 43 | else: |
| 44 | raise "no suitable function to load data" |
| 45 | |
| 46 | |
| 47 | def write_file(filename, data): |
no test coverage detected