You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
629 B
Python
28 lines
629 B
Python
5 years ago
|
import csv
|
||
|
import io
|
||
|
import os
|
||
|
|
||
|
|
||
5 years ago
|
def text_files_to_csv(files):
|
||
5 years ago
|
"""Files must be sorted lexicographically
|
||
|
Filenames must be <row>-<colum>.txt.
|
||
|
000-000.txt
|
||
|
000-001.txt
|
||
|
001-000.txt
|
||
|
etc...
|
||
|
"""
|
||
5 years ago
|
rows = []
|
||
|
for f in files:
|
||
|
directory, filename = os.path.split(f)
|
||
|
with open(f) as of:
|
||
|
txt = of.read()
|
||
|
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)
|
||
5 years ago
|
return csv_file.getvalue()
|