ireland-news-headlines/.ipynb_checkpoints/inout-checkpoint.py

25 lines
796 B
Python
Raw Normal View History

2022-05-28 17:39:32 +02:00
import csv, lzma
# Reads input from directory and returns a list
def read(dir):
X = []
if 'xz' in dir:
with lzma.open(dir) as f:
for line in f:
text = line.decode('utf-8')
text = text.replace('\n', '').split('\t')
X.append(text)
else:
with open(dir, encoding='utf-8') as f:
for line in f:
if 'tsv' in dir:
X.append(line.replace('\n', '').split('\t'))
else:
X.append(line.replace('\n', ''))
return X
# Takes the output (list) and writes it into directory
def write(output, dir):
with open(dir, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
for row in output: writer.writerow([row])