Python csv.QUOTE_MINIMAL Examples

The following are 30 code examples of csv.QUOTE_MINIMAL(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module csv , or try the search function .
Example #1
Source File: report_on_entries.py    From crowdata with MIT License 7 votes vote down vote up
def handle(self, *args, **options):
    documents = Document.objects.filter(verified=True)

    with open('docs_with_several_verified_entries.csv', 'wb') as csvfile:
      impwriter = csv.writer(csvfile, delimiter=',',
                            quotechar='"', quoting=csv.QUOTE_MINIMAL)
      # report on documents validated more than once
      for doc in documents:
        form_entries = doc.form_entries.all()
        form_fields = doc.document_set.form.all()[0].fields.filter(verify=True)
        if len(form_entries) > 2:
          verified_entry_count = 0
          for fe in form_entries:
            fe_to_dict = fe.to_dict()
            #print "-------- For entry user %s." % fe_to_dict['username']
            if fe_to_dict['answer_Adjudicatario_verified'] or \
            fe_to_dict['answer_Tipo de gasto_verified'] or \
            fe_to_dict['answer_Importe total_verified']:
              verified_entry_count += 1
          if verified_entry_count > 1:
            print "The doc %s has more than one entry verified." % doc.id
            doc.unverify()
            doc.verify()
            impwriter.writerow([doc.id, doc.verified]) 
Example #2
Source File: executor.py    From TimeMachine with GNU Lesser General Public License v3.0 6 votes vote down vote up
def output(self):
        
        with open(RunParameters.OUTPUT_FILE, "a") as csv_file:
            writer = csv.writer(csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
            while (time.time() - self.start_time) < self.time_limit:
                
                #computing snapshots
                num_snapshots = 0
                for key in self.state_graph.states:
                    if self.state_graph.states[key].solid:
                        num_snapshots = num_snapshots+1
                
                #read coverage
                coverage_manager.pull_coverage_files("temp")
                coverage_manager.compute_current_coverage()             # output in coverage.txt
                current_coverage = coverage_manager.read_current_coverage()

                # write files
                writer.writerow([str(int(time.time()-self.start_time)), str(len(self.state_graph.states)),str(num_snapshots), str(self.num_restore), str(current_coverage)])
                time.sleep(120)
                
                print "current threads:  " + str(threading.active_count())


        csv_file.close() 
Example #3
Source File: devol.py    From devol with MIT License 6 votes vote down vote up
def _record_stats(self, model, genome, loss, accuracy):
        with open(self.datafile, 'a') as csvfile:
            writer = csv.writer(csvfile, delimiter=',',
                                quotechar='"', quoting=csv.QUOTE_MINIMAL)
            row = list(genome) + [loss, accuracy]
            writer.writerow(row)

        met = loss if self._metric == 'loss' else accuracy
        if (self._bssf is -1 or
                self._metric_op(met, self._bssf) and
                accuracy is not 0):
            try:
                os.remove('best-model.h5')
            except OSError:
                pass
            self._bssf = met
            model.save('best-model.h5') 
Example #4
Source File: write_csv_files.py    From PyMIC with Apache License 2.0 6 votes vote down vote up
def create_csv_file(data_root, output_file, fields):
    """
    create a csv file to store the paths of files for each patient
    """
    filenames = []
    patient_names = os.listdir(data_root + '/' + fields[0])
    print(len(patient_names))
    for patient_name in patient_names:
        patient_image_names = []
        for field in fields:
            image_name = data_root + '/' + field + '/' + patient_name
            image_name = image_name[len(data_root) + 1 :]
            patient_image_names.append(image_name)
        filenames.append(patient_image_names)

    with open(output_file, mode='w') as csv_file:
        csv_writer = csv.writer(csv_file, delimiter=',', 
                            quotechar='"',quoting=csv.QUOTE_MINIMAL)
        csv_writer.writerow(fields)
        for item in filenames:
            csv_writer.writerow(item) 
Example #5
Source File: out_csv.py    From dataShark with GNU General Public License v3.0 6 votes vote down vote up
def __writeToCSV(self, dataRDD):
		if dataRDD.collect():
			with open(self.path, 'a') as csvfile:
				spamwriter = csv.writer(csvfile, delimiter = self.separator, quotechar = self.quote_char, quoting=csv.QUOTE_MINIMAL)
				currentTime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
				for row in dataRDD.collect():
					if self.debug:
						print row
					csvrow = [currentTime, row[0], row[1]]
					try:
						metadata = row[2]
					except:
						metadata = {}
					if metadata:
						for key in sorted(metadata):
							csvrow.append(metadata[key])
					spamwriter.writerow(csvrow)			
			print "%s - (%s) - Written %s Documents to CSV File %s " % (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), self.title, len(dataRDD.collect()), self.path)
		else:
			print "%s - (%s) - No RDD Data Recieved" % (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), self.title) 
Example #6
Source File: extract_uls_data.py    From Spectrum-Access-System with Apache License 2.0 6 votes vote down vote up
def LoadULSFlatFile(filename):
  records = {}
  header = []
  with open(filename, 'r') as data:
    reader = csv.reader(data, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    for row in reader:
      if header == []:
        header = row
      else:
        r = {}
        for i in range(0, len(header)):
          r[header[i]] = row[i]
        key = r['unique_system_identifier'] + '.' + r['location_number'] + '.' + r['antenna_number']
        records[key] = r
        if len(row) != len(header):
          print '!!! Bad record: %s' % row
  print 'Read %d records from %s' % (len(records), filename)
  return records

# This method combines the extracted data from the CSV files, maintaining only
# the most recent 'last_action_date' entry for any particular rows.
# The keys for the row are the 'unique_system_identifer', 'location_number', and
# 'antenna_number' for the sector. 
Example #7
Source File: nocolor.py    From statusparser with MIT License 6 votes vote down vote up
def success_csv(self, url, rurl, port, IP, statuscode):
		if options().csvfile:
			if not options().csvfile.endswith(".csv"):
				csvfile = f"{options().csvfile}.csv"
			else:
				csvfile = options().csvfile

			if os.path.isfile(csvfile):
				pass
			else:
				with open(csvfile, "w") as csvf:
					writer = csv.writer(csvf, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
					writer.writerow(["URL", "REDIRECT", "PORT", "IP", "STATUS CODE"])

		with open(csvfile, "a") as csvf:
			writer = csv.writer(csvf, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
			writer.writerow([url, rurl, port, IP, statuscode]) 
Example #8
Source File: write_csv_files.py    From PyMIC with Apache License 2.0 6 votes vote down vote up
def create_csv_file(data_root, output_file, fields):
    """
    create a csv file to store the paths of files for each patient
    """
    filenames = []
    patient_names = os.listdir(data_root + '/' + fields[1])
    patient_names.sort()
    print('total number of images {0:}'.format(len(patient_names)))
    for patient_name in patient_names:
        patient_image_names = []
        for field in fields:
            image_name = field + '/' + patient_name
            # if(field == 'image'):
            #     image_name = image_name.replace('_seg.', '.')
            #     #image_name = image_name[:-4]
            patient_image_names.append(image_name)
        filenames.append(patient_image_names)

    with open(output_file, mode='w') as csv_file:
        csv_writer = csv.writer(csv_file, delimiter=',', 
                            quotechar='"',quoting=csv.QUOTE_MINIMAL)
        csv_writer.writerow(fields)
        for item in filenames:
            csv_writer.writerow(item) 
Example #9
Source File: colormode.py    From statusparser with MIT License 6 votes vote down vote up
def success_csv(self, url, rurl, port, IP, statuscode):
		if options().csvfile:
			if not options().csvfile.endswith(".csv"):
				csvfile = f"{options().csvfile}.csv"
			else:
				csvfile = options().csvfile

			if os.path.isfile(csvfile):
				pass
			else:
				with open(csvfile, "w") as csvf:
					writer = csv.writer(csvf, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
					writer.writerow(["URL", "REDIRECT", "PORT", "IP", "STATUS CODE"])

		with open(csvfile, "a") as csvf:
			writer = csv.writer(csvf, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
			writer.writerow([url, rurl, port, IP, statuscode]) 
Example #10
Source File: cdc_dataset.py    From AIX360 with Apache License 2.0 6 votes vote down vote up
def _convert_xpt_to_csv(self):
        if not os.path.exists(self._csv_path):
            os.mkdir(self._csv_path)

        for i in range(len(self._cdcfiles)):
            f = self._cdcfiles[i]
            finfo = self._cdcfileinfo[i]
            xptfile = os.path.join(self._dirpath, f)
            csvfile = os.path.join(self._csv_path, f)
            csvfile = os.path.splitext(csvfile)[0]
            csvfile = csvfile + ".csv"
            if not os.path.exists(csvfile):
                print("converting ", finfo, ": ", xptfile, " to ", csvfile)
                with open(xptfile, 'rb') as in_xpt:
                    with open(csvfile, 'w',newline='') as out_csv:
                        reader = xport.Reader(in_xpt)
                        writer = csv.writer(out_csv, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
                        writer.writerow(reader.fields)
                        for row in reader:
                            writer.writerow(row) 
Example #11
Source File: test_csv.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def _test_default_attrs(self, ctor, *args):
        obj = ctor(*args)
        # Check defaults
        self.assertEqual(obj.dialect.delimiter, ',')
        self.assertEqual(obj.dialect.doublequote, True)
        self.assertEqual(obj.dialect.escapechar, None)
        self.assertEqual(obj.dialect.lineterminator, "\r\n")
        self.assertEqual(obj.dialect.quotechar, '"')
        self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL)
        self.assertEqual(obj.dialect.skipinitialspace, False)
        self.assertEqual(obj.dialect.strict, False)
        # Try deleting or changing attributes (they are read-only)
        self.assertRaises(AttributeError, delattr, obj.dialect, 'delimiter')
        self.assertRaises(AttributeError, setattr, obj.dialect, 'delimiter', ':')
        self.assertRaises(AttributeError, delattr, obj.dialect, 'quoting')
        self.assertRaises(AttributeError, setattr, obj.dialect,
                          'quoting', None) 
Example #12
Source File: write_csv_files.py    From PyMIC with Apache License 2.0 6 votes vote down vote up
def create_csv_file(data_root, output_file):
    """
    create a csv file to store the paths of files for each patient
    """
    image_folder = data_root + "/" + "training_set"
    label_folder = data_root + "/" + "training_set_label"
    filenames = os.listdir(label_folder)
    filenames = [item for item in filenames if item[0] != '.']
    file_list = []
    for filename in filenames:
        image_name = "training_set" + "/" + filename.replace("_seg.", ".")
        label_name = "training_set_label" + "/" + filename
        file_list.append([image_name, label_name])
    
    with open(output_file, mode='w') as csv_file:
        csv_writer = csv.writer(csv_file, delimiter=',', 
                            quotechar='"',quoting=csv.QUOTE_MINIMAL)
        csv_writer.writerow(["image", "label"])
        for item in file_list:
            csv_writer.writerow(item) 
Example #13
Source File: io.py    From napari with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def write_csv(
    filename: str,
    data: Union[List, np.ndarray],
    column_names: Optional[List[str]] = None,
):
    """Write a csv file.

    Parameters
    ----------
    filename : str
        Filename for saving csv.
    data : list or ndarray
        Table values, contained in a list of lists or an ndarray.
    column_names : list, optional
        List of column names for table data.
    """
    with open(filename, mode='w', newline='') as csvfile:
        writer = csv.writer(
            csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL,
        )
        if column_names is not None:
            writer.writerow(column_names)
        for row in data:
            writer.writerow(row) 
Example #14
Source File: test_csv.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_write_escape(self):
        self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
                         escapechar='\\')
        self._write_error_test(csv.Error, ['a',1,'p,"q"'],
                               escapechar=None, doublequote=False)
        self._write_test(['a',1,'p,"q"'], 'a,1,"p,\\"q\\""',
                         escapechar='\\', doublequote = False)
        self._write_test(['"'], '""""',
                         escapechar='\\', quoting = csv.QUOTE_MINIMAL)
        self._write_test(['"'], '\\"',
                         escapechar='\\', quoting = csv.QUOTE_MINIMAL,
                         doublequote = False)
        self._write_test(['"'], '\\"',
                         escapechar='\\', quoting = csv.QUOTE_NONE)
        self._write_test(['a',1,'p,q'], 'a,1,p\\,q',
                         escapechar='\\', quoting = csv.QUOTE_NONE) 
Example #15
Source File: test_csv.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_write_escape(self):
        self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
                         escapechar='\\')
        self._write_error_test(csv.Error, ['a',1,'p,"q"'],
                               escapechar=None, doublequote=False)
        self._write_test(['a',1,'p,"q"'], 'a,1,"p,\\"q\\""',
                         escapechar='\\', doublequote = False)
        self._write_test(['"'], '""""',
                         escapechar='\\', quoting = csv.QUOTE_MINIMAL)
        self._write_test(['"'], '\\"',
                         escapechar='\\', quoting = csv.QUOTE_MINIMAL,
                         doublequote = False)
        self._write_test(['"'], '\\"',
                         escapechar='\\', quoting = csv.QUOTE_NONE)
        self._write_test(['a',1,'p,q'], 'a,1,p\\,q',
                         escapechar='\\', quoting = csv.QUOTE_NONE) 
Example #16
Source File: test_csv.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def _test_default_attrs(self, ctor, *args):
        obj = ctor(*args)
        # Check defaults
        self.assertEqual(obj.dialect.delimiter, ',')
        self.assertEqual(obj.dialect.doublequote, True)
        self.assertEqual(obj.dialect.escapechar, None)
        self.assertEqual(obj.dialect.lineterminator, "\r\n")
        self.assertEqual(obj.dialect.quotechar, '"')
        self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL)
        self.assertEqual(obj.dialect.skipinitialspace, False)
        self.assertEqual(obj.dialect.strict, False)
        # Try deleting or changing attributes (they are read-only)
        self.assertRaises(AttributeError, delattr, obj.dialect, 'delimiter')
        self.assertRaises(AttributeError, setattr, obj.dialect, 'delimiter', ':')
        self.assertRaises(AttributeError, delattr, obj.dialect, 'quoting')
        self.assertRaises(AttributeError, setattr, obj.dialect,
                          'quoting', None) 
Example #17
Source File: test_csv.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_write_escape(self):
        self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
                         escapechar='\\')
        self.assertRaises(csv.Error,
                          self._write_test,
                          ['a',1,'p,"q"'], 'a,1,"p,\\"q\\""',
                          escapechar=None, doublequote=False)
        self._write_test(['a',1,'p,"q"'], 'a,1,"p,\\"q\\""',
                         escapechar='\\', doublequote = False)
        self._write_test(['"'], '""""',
                         escapechar='\\', quoting = csv.QUOTE_MINIMAL)
        self._write_test(['"'], '\\"',
                         escapechar='\\', quoting = csv.QUOTE_MINIMAL,
                         doublequote = False)
        self._write_test(['"'], '\\"',
                         escapechar='\\', quoting = csv.QUOTE_NONE)
        self._write_test(['a',1,'p,q'], 'a,1,p\\,q',
                         escapechar='\\', quoting = csv.QUOTE_NONE) 
Example #18
Source File: test_csv.py    From oss-ftp with MIT License 6 votes vote down vote up
def _test_default_attrs(self, ctor, *args):
        obj = ctor(*args)
        # Check defaults
        self.assertEqual(obj.dialect.delimiter, ',')
        self.assertEqual(obj.dialect.doublequote, True)
        self.assertEqual(obj.dialect.escapechar, None)
        self.assertEqual(obj.dialect.lineterminator, "\r\n")
        self.assertEqual(obj.dialect.quotechar, '"')
        self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL)
        self.assertEqual(obj.dialect.skipinitialspace, False)
        self.assertEqual(obj.dialect.strict, False)
        # Try deleting or changing attributes (they are read-only)
        self.assertRaises(TypeError, delattr, obj.dialect, 'delimiter')
        self.assertRaises(TypeError, setattr, obj.dialect, 'delimiter', ':')
        self.assertRaises(AttributeError, delattr, obj.dialect, 'quoting')
        self.assertRaises(AttributeError, setattr, obj.dialect,
                          'quoting', None) 
Example #19
Source File: quoting.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_bad_quote_char(self):
        data = '1,2,3'

        # Python 2.x: "...must be an 1-character..."
        # Python 3.x: "...must be a 1-character..."
        msg = '"quotechar" must be a(n)? 1-character string'
        tm.assert_raises_regex(TypeError, msg, self.read_csv,
                               StringIO(data), quotechar='foo')

        msg = 'quotechar must be set if quoting enabled'
        tm.assert_raises_regex(TypeError, msg, self.read_csv,
                               StringIO(data), quotechar=None,
                               quoting=csv.QUOTE_MINIMAL)

        msg = '"quotechar" must be string, not int'
        tm.assert_raises_regex(TypeError, msg, self.read_csv,
                               StringIO(data), quotechar=2) 
Example #20
Source File: quoting.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_null_quote_char(self):
        data = 'a,b,c\n1,2,3'

        # sanity checks
        msg = 'quotechar must be set if quoting enabled'

        tm.assert_raises_regex(TypeError, msg, self.read_csv,
                               StringIO(data), quotechar=None,
                               quoting=csv.QUOTE_MINIMAL)

        tm.assert_raises_regex(TypeError, msg, self.read_csv,
                               StringIO(data), quotechar='',
                               quoting=csv.QUOTE_MINIMAL)

        # no errors should be raised if quoting is None
        expected = DataFrame([[1, 2, 3]],
                             columns=['a', 'b', 'c'])

        result = self.read_csv(StringIO(data), quotechar=None,
                               quoting=csv.QUOTE_NONE)
        tm.assert_frame_equal(result, expected)

        result = self.read_csv(StringIO(data), quotechar='',
                               quoting=csv.QUOTE_NONE)
        tm.assert_frame_equal(result, expected) 
Example #21
Source File: test_csv.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_write_escape(self):
        self._write_test(['a',1,'p,q'], 'a,1,"p,q"',
                         escapechar='\\')
        self.assertRaises(csv.Error,
                          self._write_test,
                          ['a',1,'p,"q"'], 'a,1,"p,\\"q\\""',
                          escapechar=None, doublequote=False)
        self._write_test(['a',1,'p,"q"'], 'a,1,"p,\\"q\\""',
                         escapechar='\\', doublequote = False)
        self._write_test(['"'], '""""',
                         escapechar='\\', quoting = csv.QUOTE_MINIMAL)
        self._write_test(['"'], '\\"',
                         escapechar='\\', quoting = csv.QUOTE_MINIMAL,
                         doublequote = False)
        self._write_test(['"'], '\\"',
                         escapechar='\\', quoting = csv.QUOTE_NONE)
        self._write_test(['a',1,'p,q'], 'a,1,p\\,q',
                         escapechar='\\', quoting = csv.QUOTE_NONE) 
Example #22
Source File: test_csv.py    From BinderFilter with MIT License 6 votes vote down vote up
def _test_default_attrs(self, ctor, *args):
        obj = ctor(*args)
        # Check defaults
        self.assertEqual(obj.dialect.delimiter, ',')
        self.assertEqual(obj.dialect.doublequote, True)
        self.assertEqual(obj.dialect.escapechar, None)
        self.assertEqual(obj.dialect.lineterminator, "\r\n")
        self.assertEqual(obj.dialect.quotechar, '"')
        self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL)
        self.assertEqual(obj.dialect.skipinitialspace, False)
        self.assertEqual(obj.dialect.strict, False)
        # Try deleting or changing attributes (they are read-only)
        self.assertRaises(TypeError, delattr, obj.dialect, 'delimiter')
        self.assertRaises(TypeError, setattr, obj.dialect, 'delimiter', ':')
        self.assertRaises(AttributeError, delattr, obj.dialect, 'quoting')
        self.assertRaises(AttributeError, setattr, obj.dialect,
                          'quoting', None) 
Example #23
Source File: utils.py    From AmbroBot with GNU General Public License v3.0 6 votes vote down vote up
def dump_results_to_csv(final_result, result_steps, decimal_precision, error_minimo):
    FILE_PATH = 'aproximation_result.csv'
    solution_len = len(final_result)

    with open(FILE_PATH, mode='w') as f:
        csv_writer = csv.writer(
            f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL
        )

        coordinates = [f'X_{i}' for i in range(solution_len)]
        csv_writer.writerow(
            ['i'] + coordinates + ['Norma 1', 'Norma 2', 'Norma 3', 'Criterio de Paro']
        )

        for i, step in enumerate(result_steps):
            array_elems = [round(number, decimal_precision) for number in step[0]]
            normas = [
                round(norma, decimal_precision) if norma != '-' else '-'
                for norma in step[1:]
            ]
            row = [i] + array_elems + normas + [error_minimo]
            csv_writer.writerow(row)

    return FILE_PATH 
Example #24
Source File: test_csv.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def _test_default_attrs(self, ctor, *args):
        obj = ctor(*args)
        # Check defaults
        self.assertEqual(obj.dialect.delimiter, ',')
        self.assertEqual(obj.dialect.doublequote, True)
        self.assertEqual(obj.dialect.escapechar, None)
        self.assertEqual(obj.dialect.lineterminator, "\r\n")
        self.assertEqual(obj.dialect.quotechar, '"')
        self.assertEqual(obj.dialect.quoting, csv.QUOTE_MINIMAL)
        self.assertEqual(obj.dialect.skipinitialspace, False)
        self.assertEqual(obj.dialect.strict, False)
        # Try deleting or changing attributes (they are read-only)
        self.assertRaises(TypeError, delattr, obj.dialect, 'delimiter')
        self.assertRaises(TypeError, setattr, obj.dialect, 'delimiter', ':')
        self.assertRaises(AttributeError, delattr, obj.dialect, 'quoting')
        self.assertRaises(AttributeError, setattr, obj.dialect,
                          'quoting', None) 
Example #25
Source File: check_mi.py    From check-media-integrity with GNU General Public License v3.0 5 votes vote down vote up
def save_csv(filename, data):
    with open(filename, mode='w') as out_file:
        out_writer = csv.writer(out_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        for entry in data:
            out_writer.writerow(list(entry)) 
Example #26
Source File: __init__.py    From starthinker with Apache License 2.0 5 votes vote down vote up
def rows_to_csv(rows):
  csv_string = StringIO()
  writer = csv.writer(csv_string, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
  count = 0
  for row_number, row in enumerate(rows):
    try:
      writer.writerow(row)
      count += 1
    except Exception as e:
      print('Error:', row_number, str(e), row)
  csv_string.seek(0)  # important otherwise contents is zero
  if project.verbose: print('CSV Rows Written:', count)
  return csv_string 
Example #27
Source File: Csv.py    From RTGraph with MIT License 5 votes vote down vote up
def run(self):
        """
        Process will monitor the internal buffer to write data to the export file,
        and the process will loop again after timeout if more data is available.
        :return:
        """
        Log.i(TAG, "Process starting...")
        self._csv = csv.writer(self._file, delimiter=Constants.csv_delimiter, quoting=csv.QUOTE_MINIMAL)
        while not self._exit.is_set():
            self._consume_queue()
            sleep(self._timeout)
        # last check on the queue to completely remove data.
        self._consume_queue()
        Log.i(TAG, "Process finished")
        self._file.close() 
Example #28
Source File: views.py    From pingAdmin with GNU General Public License v3.0 5 votes vote down vote up
def get(self, *args, **kwargs):
        # 定义导出csv字段
        fields = [
            field for field in AssetInfo._meta.fields
            # 选择已勾选的字段
            if field.name in list(self.request.GET.dict().keys())
        ]
        export_scope = self.request.GET.get('exportScope')
        group_id_list = self.request.GET.getlist('groupIdList')
        asset_id = self.request.GET.get('assetId')
        asset_id_list = [] if not asset_id else asset_id.split(',')

        # 定义导出文件名及导出资产
        filename = 'asset.csv'

        if export_scope == 'part' and asset_id_list:
            assets = AssetInfo.objects.filter(Q(groups_id__in=group_id_list) & Q(id__in=asset_id_list))
        else:
            assets = AssetInfo.objects.filter(groups_id__in=group_id_list)

        # 声明一个csv的响应
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="%s"' % filename

        # csv的响应的编码格式声明
        response.write(codecs.BOM_UTF8)
        writer = csv.writer(response, dialect='excel', quoting=csv.QUOTE_MINIMAL)

        # 定义标题字段并写入内容
        header = [field.verbose_name for field in fields]
        writer.writerow(header)

        # 循环写入资产信息
        for asset in assets:
            data = [getattr(asset, field.name) for field in fields]
            writer.writerow(data)
        return response 
Example #29
Source File: commonssql.py    From wikiteam with GNU General Public License v3.0 5 votes vote down vote up
def main():
    year = int(sys.argv[1])
    filename = 'commonssql-%s.csv' % (year)
    f = open(filename, 'w')
    f.write('img_name|img_timestamp|img_user|img_user_text|img_size|img_width|img_height\n')
    f.close()

    #http://www.mediawiki.org/wiki/Manual:Image_table
    #http://www.mediawiki.org/wiki/Manual:Oldimage_table
    
    queries = [
        "SELECT /* commonssql.py SLOW_OK */ img_name, img_timestamp, img_user, img_user_text, img_size, img_width, img_height FROM image WHERE img_timestamp>=%d0101000000 AND img_timestamp<=%d1231235959 ORDER BY img_timestamp ASC" % (year, year),
        "SELECT /* commonssql.py SLOW_OK */ oi_archive_name AS img_name, oi_timestamp AS img_timestamp, oi_user AS img_user, oi_user_text AS img_user_text, oi_size AS img_size, oi_width AS img_width, oi_height AS img_height FROM oldimage WHERE oi_deleted=0 AND oi_timestamp>=%d0101000000 AND oi_timestamp<=%d1231235959 ORDER BY oi_timestamp ASC" % (year, year), #do not get unavailable images
    ]

    f = csv.writer(open(filename, 'a'), delimiter='|', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    conn = MySQLdb.connect(host='s4.labsdb', db='commonswiki_p', read_default_file='~/replica.my.cnf', use_unicode=True)
    for query in queries:
        conn.query(query)
        r = conn.store_result()
        c = 0
        row = r.fetch_row(maxrows=1, how=1)
        rows = []
        while row:
            if len(row) == 1:
                img_name = re.sub(' ', '_', row[0]['img_name'])
                img_timestamp = row[0]['img_timestamp']
                img_user = row[0]['img_user']
                img_user_text = re.sub(' ', '_', row[0]['img_user_text'])
                img_size = row[0]['img_size']
                img_width = row[0]['img_width']
                img_height = row[0]['img_height']
                
                rows.append([img_name, img_timestamp, img_user, img_user_text, img_size, img_width, img_height])
                c += 1
                if c % 10000 == 0:
                    print(c)
                    f.writerows(rows)
                    rows = []
            row = r.fetch_row(maxrows=1, how=1)
        f.writerows(rows) 
Example #30
Source File: __init__.py    From starthinker with Apache License 2.0 5 votes vote down vote up
def csv_to_rows(csv_string):
  if csv_string:
    csv.field_size_limit(sys.maxsize)
    if isinstance(csv_string, str): csv_string = StringIO(csv_string)
    for row in csv.reader(csv_string, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL, skipinitialspace=True, escapechar='\\'):
      yield row