Compare commits
2 commits
master
...
csvDirecto
Author | SHA1 | Date | |
---|---|---|---|
|
8ceb2d05c3 | ||
|
f7f14090ed |
1 changed files with 40 additions and 0 deletions
40
templates/reorganize.py
Normal file
40
templates/reorganize.py
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import os
|
||||||
|
import csv
|
||||||
|
import itertools
|
||||||
|
import shutil
|
||||||
|
import errno
|
||||||
|
|
||||||
|
# TODO: filter out quotes which are formatted incorrectly before reorganizing
|
||||||
|
|
||||||
|
quotes = []
|
||||||
|
for root, dirs, files in os.walk('quotes/'):
|
||||||
|
for name in files:
|
||||||
|
(base, ext) = os.path.splitext(name)
|
||||||
|
if ext == '.csv':
|
||||||
|
full_name = os.path.join(root, name)
|
||||||
|
with open(full_name) as csvfile:
|
||||||
|
csvreader = csv.reader(csvfile, delimiter=',', quotechar='`', skipinitialspace=True)
|
||||||
|
quotes += list(csvreader)
|
||||||
|
|
||||||
|
# TODO: back up the quotes directory and implement recovery if the quote writing fails
|
||||||
|
shutil.rmtree('quotes')
|
||||||
|
|
||||||
|
for quote in quotes:
|
||||||
|
# path: ./quotes/author/work/
|
||||||
|
path = 'quotes/' + quote[1] + '/'
|
||||||
|
|
||||||
|
# https://stackoverflow.com/a/600612/11736197
|
||||||
|
try:
|
||||||
|
os.makedirs(path)
|
||||||
|
except OSError as exc:
|
||||||
|
if exc.errno == errno.EEXIST and os.path.isdir(path):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
# file path: ./quotes/author/title.csv
|
||||||
|
with open(path + quote[3] + '.csv', 'a') as csvfile:
|
||||||
|
writer = csv.writer(csvfile, delimiter=',', quotechar='`', quoting=csv.QUOTE_ALL)
|
||||||
|
writer.writerow(quote)
|
Loading…
Reference in a new issue