def _writeToCSV(self):
'''
INFO
----
Writes a 2-dimensional list to a CSV text file
Comma-delimits values. If there is no data, then there is no attempt to
creat a file.
RETURNS
-------
None
'''
if self._dataAsList:
with open(self._filePathAndName,'w') as csvFile:
writer = csv.writer(csvFile, lineterminator='\n', quoting=csv.QUOTE_NONNUMERIC )
writer.writerows(self._dataAsList)
csvFile.close()
python类QUOTE_NONNUMERIC的实例源码
def clear(filename):
rows = []
with open(filename, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=';')
rows = [row for row in reader]
with open(filename, 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=';', quotechar='"', skipinitialspace=True,
quoting=csv.QUOTE_NONNUMERIC) # QUOTE_MINIMAL vs QUOTE_NONNUMERIC
for row in rows:
if not row[0].startswith("#"):
# ??????? ??????????? ??????? ? ????????? ?????????
row[2] = row[2].strip(' ')
writer.writerow(row)
def get_csv_stats(stats, recorded_cols=None):
"""
Create a CSV buffer from the stats DataFrame.
Parameters
----------
path: str
stats: list[Object]
recorded_cols: list[str]
Returns
-------
"""
df, columns = prepare_stats(stats, recorded_cols=recorded_cols)
return df.to_csv(
None,
columns=columns,
# encoding='utf-8',
quoting=csv.QUOTE_NONNUMERIC
).encode()
def getFeatures(self):
csv_files = self.experiment.getFeaturesFilesFullpaths()
features_names = []
features = None
for csv_file in csv_files:
with open(csv_file, 'r') as f:
header = f.readline().strip('\n').split(',')[1:]
features_names += header
current_features = list(list(rec) for rec in csv.reader(f,
quoting = csv.QUOTE_NONNUMERIC))
if features is None:
features = [l[1:] for l in current_features]
else:
features = [f1 + f2[1:] for f1, f2 in zip(features, current_features)]
features = np.array(features)
return features, features_names
def query_to_csv(cursor, cols, outname):
"""
Turns the query into a CSV file for the GPA calculation.
:param cursor: the database cursor
:type cursor: DictCursor
:param cols: the header names
:type cols: list
:param outname: the CSV output filename
:type outname: str
"""
logger.info("Generating CSV: {0}".format(outname))
with open(outname, 'w') as outfile:
writer = csv.writer(outfile, quoting=csv.QUOTE_NONNUMERIC)
writer.writerow(cols.split(","))
for row in cursor.fetchall():
writer.writerow(row)
outfile.flush()
logger.info("Generated CSV ({0}) exists: ".format(outname, os.path.isfile(outname)))
common.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 43
收藏 0
点赞 0
评论 0
def writerow(self, row):
def _check_as_is(x):
return (self.quoting == csv.QUOTE_NONNUMERIC and
is_number(x)) or isinstance(x, str)
row = [x if _check_as_is(x)
else pprint_thing(x).encode("utf-8") for x in row]
self.writer.writerow([s for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
self.stream.write(data)
# empty queue
self.queue.truncate(0)
common.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def writerows(self, rows):
def _check_as_is(x):
return (self.quoting == csv.QUOTE_NONNUMERIC and
is_number(x)) or isinstance(x, str)
for i, row in enumerate(rows):
rows[i] = [x if _check_as_is(x)
else pprint_thing(x).encode("utf-8") for x in row]
self.writer.writerows([[s for s in row] for row in rows])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
self.stream.write(data)
# empty queue
self.queue.truncate(0)
def save_csv(matrix, output_directory, output_file_name):
"""
Saves the input matrix as a CSV File
Args:
matrix(list): an array containing data to be saved
output_drectory(str): location to save graph
output_file_name(str): name of the csv file to be saved
Returns:
null
"""
if config.DEBUGGER:
print "Generating", (output_file_name + ".csv")
check_if_dir_exists(output_directory) #create output directory if doesn't exist
output_file = output_directory + "/" + output_file_name +".csv"
with open(output_file, 'wb') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_NONNUMERIC)
if matrix is not None:
for col in matrix:
wr.writerow(col)
def writecsv(estimates, filepath, append=False):
"""
Write a CSV file of estimates.
If append = True, I do NOT overwrite an existing file, but append to it !
"""
if append:
f = open(filepath, "a")
else:
f = open(filepath, "w")
writer = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
for est in estimates:
#est.td = "%.5f" % (est.td)
#est.tderr = "%.5f" % (est.tderr)
#est.ms = "%.5f" % (est.ms)
#est.timetaken = "%.5f" % (est.timetaken)
writer.writerow(est.aslist())
f.close()
print "Wrote %i estimates into %s" % (len(estimates), filepath)
graphbuilder.py 文件源码
项目:graph_tools_networkx
作者: AirtrafficAnalysisGroup
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def build_graph_for_file(file_path, dir_name, name):
data = open(file_path, 'r')
G=nx.DiGraph()
rows = csv.reader(data, quoting=csv.QUOTE_NONNUMERIC)
next(rows) #skip the header
for row in rows:
row_fil = list(filter(lambda x: type(x) is float, row))
if G.has_node(row_fil[0]) is not True:
G.add_node(row_fil[0], market_id=row_fil[1])
if G.has_node(row_fil[2]) is not True:
G.add_node(row_fil[2], market_id=row_fil[3])
if G.has_edge(row_fil[0], row_fil[2]):
old = G.get_edge_data(row_fil[0], row_fil[2])
G.add_edge(row_fil[0], row_fil[2], num_of_people=old['num_of_people'] + row_fil[4], total_price=old['total_price'] + row_fil[5])
else:
G.add_edge(row_fil[0], row_fil[2], num_of_people=row_fil[4], total_price=row_fil[5])
output_file_path = ('graphs/' + name + '.gexf')
nx.write_gexf(G, output_file_path)
def render(self, form_data: dict):
output = io.StringIO()
writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC, delimiter=",")
tz = pytz.timezone(self.event.settings.timezone)
headers = [
_('Order code'), _('Order date'), _('Invoices'), _('SEPA export date'), _('Payment amount')
]
writer.writerow(headers)
qs = SepaExportOrder.objects.filter(export__event=self.event).order_by('export__datetime').select_related(
'export', 'order'
).prefetch_related('order__invoices')
for seo in qs:
row = [
seo.order.code,
seo.order.datetime.astimezone(tz).strftime('%Y-%m-%d'),
', '.join([i.number for i in seo.order.invoices.all()]),
seo.export.datetime.astimezone(tz).strftime('%Y-%m-%d %H:%M:%S'),
seo.amount,
]
writer.writerow(row)
return 'sepaexports.csv', 'text/csv', output.getvalue().encode("utf-8")
def _build_file(self):
self.total_rows = 0
#Get recent tweets from dril and add to new file
for status in tweepy.Cursor(api.user_timeline, 'dril', since_id=self.since).items():
self.total_rows += self._process_status(status)
#Put content of old file in new file
#This is kind of messy uhhh
try:
#Open things for reading and writing
readFile = open('data/dril.csv', 'rt', encoding='utf-8')
writeFile = open('data/new.csv', 'at', encoding='utf-8')
read = reader(readFile)
write = writer(writeFile, delimiter=',', quoting=QUOTE_NONNUMERIC) #Uhhhhmmmmmhmh mmmm
for row in read:
write.writerow([int(row[0]), row[1]])
self.total_rows += 1
except IOError:
print('Failed to open file (1) [okay if this is the first time running]')
#Rename the new file to be the old file
os.rename('data/new.csv', 'data/dril.csv')
def make_csv(edata, verbose=None):
fieldnames = ["id", 'doc_number', 'doc_date', 'doc_v_date', "trans_date",
"amount", "payer_edrpou", "payer_name", 'payer_account',
"payer_mfo", "payer_bank", "recipt_edrpou", "recipt_name",
'recipt_account', "recipt_mfo", "recipt_bank", "region_id",
'doc_add_attr', "payment_details",]
try:
with open('edata.csv', 'w') as csvfile:
writer = csv.DictWriter(
csvfile, fieldnames=fieldnames,
delimiter=';', quoting=csv.QUOTE_NONNUMERIC
)
writer.writeheader()
for row in edata:
writer.writerow(row)
if verbose:
sys.stdout.write("{} ?????? ????????\n".format(len(edata)))
except:
raise
def to_csv(self, fileobj, header=False, totals=False, delimiter=';',
quotechar='"', quoting=csv.QUOTE_NONNUMERIC,
escapechar='', extra_rows=None, **kwargs):
writer = csv.writer(fileobj, delimiter=str(delimiter),
quotechar=str(quotechar), quoting=quoting,
escapechar=str(escapechar), **kwargs)
if extra_rows is not None:
writer.writerows(extra_rows)
if header:
writer.writerow([name.encode(settings.DEFAULT_CHARSET) for name, _ in self.get_fields()])
for record in self.iter_results():
writer.writerow([elem.encode(settings.DEFAULT_CHARSET) if isinstance(elem, unicode) else elem
for elem in record])
if totals and self.get_has_totals():
writer.writerow(self.totals)
def run(self):
output = open(self.output, 'w', newline="\n", encoding="utf-8")
threads = requests.get(self.uri).json()
if 'error' in threads:
print(threads)
return
fieldnames = ['from_id', 'from', 'time', 'message', 'attachments', 'shares', 'url']
self.writer = csv.DictWriter(output, dialect='excel', fieldnames=fieldnames, extrasaction='ignore', quoting=csv.QUOTE_NONNUMERIC)
self.writer.writerow(dict((n, n) for n in fieldnames))
self.scrape_thread_list(threads, 5)
output.close()
def __init__(self, outputFile):
self.writer = csv.writer(
outputFile,
quoting=csv.QUOTE_NONNUMERIC,
)
self.group_name = ''
def insert(self, table, columns, types, values, primary_key_index=[], is_orreplace=False, is_commit=True):
"""
Insert into the table
:param table: Table name
:param columns: Column array
:param types: Type array
:param values: Value array
:param primary_key_index: An array of indices of primary keys in columns,
e.g. [0] means the first column is the primary key
:param is_orreplace: Indicate if the query is "INSERT OR REPLACE"
"""
ret = True
file_path = os.path.join(self.file_directory, table + ".csv")
if len(columns) != len(values):
return False
self.lock.acquire()
if not os.path.isfile(file_path):
ret = False
else:
with open(file_path, "a+") as csvfile:
writer = csv.writer(csvfile, lineterminator='\n', quotechar='\"', quoting=csv.QUOTE_NONNUMERIC)
writer.writerow(values)
self.lock.release()
if not ret:
raise Exception("File (%s) has not been created.")
return True
def csvWrite(filePath, data):
with open(filePath, 'a') as fd:
writer = csv.writer(fd, delimiter=',', quoting=csv.QUOTE_NONNUMERIC)
writer.writerow(data)
def get_csv(self):
csvout = StringIO()
csvwriter = csv.writer(
csvout,
dialect='excel',
quoting=csv.QUOTE_NONNUMERIC
)
csvwriter.writerow(self.column_names)
for row in self.rows:
row_formatted = []
for cell in row:
if isinstance(cell, datetime.datetime):
cell = cell.strftime('%Y-%m-%d %H:%M')
elif isinstance(cell, (int, long)):
cell = str(cell)
elif isinstance(cell, (list, tuple)):
cell = str(cell)
elif cell is None:
cell = ''
else:
cell = cell.encode('utf8')
row_formatted.append(cell)
try:
csvwriter.writerow(row_formatted)
except Exception, e:
raise Exception("%s: %s, %s"%(e, row, row_formatted))
csvout.seek(0)
return csvout.read()
def test_write_quoting(self):
self._write_test(['a',1,'p,q'], 'a,1,"p,q"')
self.assertRaises(csv.Error,
self._write_test,
['a',1,'p,q'], 'a,1,p,q',
quoting = csv.QUOTE_NONE)
self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
quoting = csv.QUOTE_MINIMAL)
self._write_test(['a',1,'p,q'], '"a",1,"p,q"',
quoting = csv.QUOTE_NONNUMERIC)
self._write_test(['a',1,'p,q'], '"a","1","p,q"',
quoting = csv.QUOTE_ALL)
self._write_test(['a\nb',1], '"a\nb","1"',
quoting = csv.QUOTE_ALL)
def test_read_quoting(self):
self._read_test(['1,",3,",5'], [['1', ',3,', '5']])
self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']],
quotechar=None, escapechar='\\')
self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']],
quoting=csv.QUOTE_NONE, escapechar='\\')
# will this fail where locale uses comma for decimals?
self._read_test([',3,"5",7.3, 9'], [['', 3, '5', 7.3, 9]],
quoting=csv.QUOTE_NONNUMERIC)
self._read_test(['"a\nb", 7'], [['a\nb', ' 7']])
self.assertRaises(ValueError, self._read_test,
['abc,3'], [[]],
quoting=csv.QUOTE_NONNUMERIC)
def test_write_quoting(self):
self._write_test(['a',1,'p,q'], 'a,1,"p,q"')
self._write_error_test(csv.Error, ['a',1,'p,q'],
quoting = csv.QUOTE_NONE)
self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
quoting = csv.QUOTE_MINIMAL)
self._write_test(['a',1,'p,q'], '"a",1,"p,q"',
quoting = csv.QUOTE_NONNUMERIC)
self._write_test(['a',1,'p,q'], '"a","1","p,q"',
quoting = csv.QUOTE_ALL)
self._write_test(['a\nb',1], '"a\nb","1"',
quoting = csv.QUOTE_ALL)
def test_write_float(self):
# Issue 13573: loss of precision because csv.writer
# uses str() for floats instead of repr()
orig_row = [1.234567890123, 1.0/7.0, 'abc']
f = StringIO()
c = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
c.writerow(orig_row)
f.seek(0)
c = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
new_row = next(c)
self.assertEqual(orig_row, new_row)
def test_read_quoting(self):
self._read_test(['1,",3,",5'], [['1', ',3,', '5']])
self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']],
quotechar=None, escapechar='\\')
self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']],
quoting=csv.QUOTE_NONE, escapechar='\\')
# will this fail where locale uses comma for decimals?
self._read_test([',3,"5",7.3, 9'], [['', 3, '5', 7.3, 9]],
quoting=csv.QUOTE_NONNUMERIC)
self._read_test(['"a\nb", 7'], [['a\nb', ' 7']])
self.assertRaises(ValueError, self._read_test,
['abc,3'], [[]],
quoting=csv.QUOTE_NONNUMERIC)
def test_write_quoting(self):
self._write_test(['a',1,'p,q'], 'a,1,"p,q"')
self._write_error_test(csv.Error, ['a',1,'p,q'],
quoting = csv.QUOTE_NONE)
self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
quoting = csv.QUOTE_MINIMAL)
self._write_test(['a',1,'p,q'], '"a",1,"p,q"',
quoting = csv.QUOTE_NONNUMERIC)
self._write_test(['a',1,'p,q'], '"a","1","p,q"',
quoting = csv.QUOTE_ALL)
self._write_test(['a\nb',1], '"a\nb","1"',
quoting = csv.QUOTE_ALL)
def test_write_float(self):
# Issue 13573: loss of precision because csv.writer
# uses str() for floats instead of repr()
orig_row = [1.234567890123, 1.0/7.0, 'abc']
f = StringIO()
c = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
c.writerow(orig_row)
f.seek(0)
c = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
new_row = next(c)
self.assertEqual(orig_row, new_row)
def test_read_quoting(self):
self._read_test(['1,",3,",5'], [['1', ',3,', '5']])
self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']],
quotechar=None, escapechar='\\')
self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']],
quoting=csv.QUOTE_NONE, escapechar='\\')
# will this fail where locale uses comma for decimals?
self._read_test([',3,"5",7.3, 9'], [['', 3, '5', 7.3, 9]],
quoting=csv.QUOTE_NONNUMERIC)
self._read_test(['"a\nb", 7'], [['a\nb', ' 7']])
self.assertRaises(ValueError, self._read_test,
['abc,3'], [[]],
quoting=csv.QUOTE_NONNUMERIC)
run_experiments.py 文件源码
项目:Effective-Amazon-Machine-Learning
作者: PacktPublishing
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def run_experiment(experiment):
'''
This function is the main function
It takes a string 'experiment' that drives the naming of the files
- it generates the filenames for the datasources, schemas, recipes
- it sends a randomized version of the data to an s3 bucket
- creates the train, valid and test datasources
- creates the model
- creates the evaluation
'''
# start by generating all teh filenames for the current experiment
filenames = generate_filenames(experiment)
# load the data form local, shuffle and save back to original file
df = pd.read_csv(filepath + filenames['main'])
df = df.reindex(np.random.permutation(df.index))
df.to_csv(filepath + filenames['main'], quoting= csv.QUOTE_NONNUMERIC, index=False)
# sends the original file to s3
os.system("aws s3 cp %s%s %s "% (filepath, filenames['main'], s3_path) )
# write cli JSON
create_dsrc("train", 0, 60)
create_dsrc("valid", 60, 80)
create_dsrc("test", 80, 100)
create_model()
create_eval("valid")
create_eval("test")
# ----------------------------------------------------------------------------
# datasource, model, evaluation and batch predictions functions
# These functions all do the same thing
# 1. write the JSON parameters to a JSON formatted file in json_path folder
# 2. execute the AWS CLI command that will create the object: datasource, model, ...
# ----------------------------------------------------------------------------
def test_write_quoting(self):
self._write_test(['a',1,'p,q'], 'a,1,"p,q"')
self.assertRaises(csv.Error,
self._write_test,
['a',1,'p,q'], 'a,1,p,q',
quoting = csv.QUOTE_NONE)
self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
quoting = csv.QUOTE_MINIMAL)
self._write_test(['a',1,'p,q'], '"a",1,"p,q"',
quoting = csv.QUOTE_NONNUMERIC)
self._write_test(['a',1,'p,q'], '"a","1","p,q"',
quoting = csv.QUOTE_ALL)
self._write_test(['a\nb',1], '"a\nb","1"',
quoting = csv.QUOTE_ALL)
def test_read_quoting(self):
self._read_test(['1,",3,",5'], [['1', ',3,', '5']])
self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']],
quotechar=None, escapechar='\\')
self._read_test(['1,",3,",5'], [['1', '"', '3', '"', '5']],
quoting=csv.QUOTE_NONE, escapechar='\\')
# will this fail where locale uses comma for decimals?
self._read_test([',3,"5",7.3, 9'], [['', 3, '5', 7.3, 9]],
quoting=csv.QUOTE_NONNUMERIC)
self._read_test(['"a\nb", 7'], [['a\nb', ' 7']])
self.assertRaises(ValueError, self._read_test,
['abc,3'], [[]],
quoting=csv.QUOTE_NONNUMERIC)