Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def write(self, texts, outfile):
writer = unicodecsv.writer(outfile, quoting=unicodecsv.QUOTE_NONNUMERIC, encoding='utf-8')
writer.writerow(texts[0].__dict__.keys())
writer.writerows( [text.__dict__.values() for text in texts] )
if os.path.exists(outfile):
shutil.move(outfile, outfile + '.bak')
csvfile = file(outfile, 'w')
else:
csvfile = sys.stdout
if not no_comments:
csvfile.write(comments)
csvfile.write('#' * 80 + '\n')
csvwriter = unicodecsv.DictWriter(csvfile,
fieldnames=['Source', 'Node', 'Memo', 'Text', 'Fragment'],
extrasaction='ignore',
lineterminator=os.linesep,
quoting=unicodecsv.QUOTE_NONNUMERIC)
csvwriter.writeheader()
for tagging in intersection:
tagging['Fragment'] = str(tagging['Start']) + ':' + str(tagging['End'])
tagging['Text'] = tagging['Content'][tagging['Start']-1:tagging['End']]
tagging['Node'] = os.linesep.join(nodeiter for nodeiter in tagging['NodeTuple'] if nodeiter not in node)
csvwriter.writerows(intersection)
csvfile.close()
except:
raise
finally:
del norm
def next(self):
row = self.source.next()
self.buffer.truncate(0)
self.buffer.seek(0)
if not self.csv:
self.csv = csv.DictWriter(self.buffer, row.keys(), quoting=csv.QUOTE_NONNUMERIC)
self.add_header = True
if self.add_header:
if hasattr(self.csv, 'writeheader'):
self.csv.writeheader()
else:
self.csv.writerow(dict((fn, fn) for fn in self.csv.fieldnames))
self.add_header = False
self.csv.writerow(row)
self.buffer.seek(0)
return self.buffer.read()
from cliff import columns
import six
if sys.version_info[0] == 3:
import csv
else:
import unicodecsv as csv
class CSVLister(ListFormatter):
QUOTE_MODES = {
'all': csv.QUOTE_ALL,
'minimal': csv.QUOTE_MINIMAL,
'nonnumeric': csv.QUOTE_NONNUMERIC,
'none': csv.QUOTE_NONE,
}
def add_argument_group(self, parser):
group = parser.add_argument_group('CSV Formatter')
group.add_argument(
'--quote',
choices=sorted(self.QUOTE_MODES.keys()),
dest='quote_mode',
default='nonnumeric',
help='when to include quotes, defaults to nonnumeric',
)
def emit_list(self, column_names, data, stdout, parsed_args):
writer_kwargs = dict(
quoting=self.QUOTE_MODES[parsed_args.quote_mode],
if os.path.exists(ofn) and os.path.exists( ofncsv_lfp ):
tables = bqutil.get_list_of_table_ids(dataset)
if not 'forum' in tables:
print "Already done? But no forums table loaded into datasaet %s. Redoing." % dataset
else:
print "Already done %s -> %s (skipping)" % (fn, ofn)
print "Already done %s -> %s (skipping)" % (fn, ofncsv_lfp)
sys.stdout.flush()
return
print "Processing %s -> writing to %s and %s (%s)" % (fn, ofn, ofncsv, datetime.datetime.now())
sys.stdout.flush()
# Setup CSV header
ocsv = csv.DictWriter( openfile(ofncsv, 'w'), fieldnames=SCHEMA_DICT.keys(), quoting=csv.QUOTE_NONNUMERIC )
ocsv.writeheader()
cnt = 0
ofp = gzip.GzipFile('tmp.json.gz', 'w')
data = OrderedDict()
for line in fp:
cnt += 1
# Write JSON row
newline = do_rephrase_line(line, linecnt=cnt)
ofp.write(newline)
try:
#Write CSV row
data = json.loads(newline)
ocsv.writerow( data )
except Exception as err: