import csv import io import os def text_files_to_csv(files): """Files must be sorted lexicographically Filenames must be -.txt. 000-000.txt 000-001.txt 001-000.txt etc... """ rows = [] for f in files: directory, filename = os.path.split(f) with open(f) as of: txt = of.read().strip() row, column = map(int, filename.split(".")[0].split("-")) if row == len(rows): rows.append([]) rows[row].append(txt) csv_file = io.StringIO() writer = csv.writer(csv_file) writer.writerows(rows) return csv_file.getvalue() def main(files): return text_files_to_csv(files)