Python csv.writer方法代码示例

本文整理汇总了Python中csv.writer方法的典型用法代码示例。如果您正苦于以下问题:Python csv.writer方法的具体用法?Python csv.writer怎么用?Python csv.writer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在模块csv的用法示例。

在下文中一共展示了csv.writer方法的30个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: write

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def write(self, epoch, step, data):
        if not self.ks:
            return
        # Make sure that keys have no underscores.
        data = {k.replace('_', '-'):v for k, v in data.items()}
        row = [epoch, step]
        for k in self.ks:
            if k in data:
                row.append(_toscalar(data[k]))
                self.prev[k] = data[k]
            elif k in self.prev:
                row.append(_toscalar(self.prev[k]))
            else:
                row.append(None)
        self.writer.writerow(row) 
开发者ID:mme,项目名称:vergeml,代码行数:20,代码来源:env.py


示例2: save_id_to_path_mapping

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def save_id_to_path_mapping(self):
    """Saves mapping from submission IDs to original filenames.
    This mapping is saved as CSV file into target directory.
    """
    if not self.id_to_path_mapping:
      return
    with open(self.local_id_to_path_mapping_file, 'w') as f:
      writer = csv.writer(f)
      writer.writerow(['id', 'path'])
      for k, v in sorted(iteritems(self.id_to_path_mapping)):
        writer.writerow([k, v])
    cmd = ['gsutil', 'cp', self.local_id_to_path_mapping_file,
           os.path.join(self.target_dir, 'id_to_path_mapping.csv')]
    if subprocess.call(cmd) != 0:
      logging.error('Can\'t copy id_to_path_mapping.csv to target directory') 
开发者ID:StephanZheng,项目名称:neural-fingerprinting,代码行数:18,代码来源:validate_and_copy_submissions.py


示例3: _save_sorted_results

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def _save_sorted_results(self, run_stats, scores, image_count, filename):
    """Saves sorted (by score) results of the evaluation.
    Args:
      run_stats: dictionary with runtime statistics for submissions,
        can be generated by WorkPiecesBase.compute_work_statistics
      scores: dictionary mapping submission ids to scores
      image_count: dictionary with number of images processed by submission
      filename: output filename
    """
    with open(filename, 'w') as f:
      writer = csv.writer(f)
      writer.writerow(['SubmissionID', 'ExternalTeamId', 'Score',
                       'MedianTime', 'ImageCount'])
      get_second = lambda x: x[1]
      for s_id, score in sorted(iteritems(scores),
                                key=get_second, reverse=True):
        external_id = self.submissions.get_external_id(s_id)
        stat = run_stats.get(
            s_id, collections.defaultdict(lambda: float('NaN')))
        writer.writerow([s_id, external_id, score,
                         stat['median_eval_time'],
                         image_count[s_id]]) 
开发者ID:StephanZheng,项目名称:neural-fingerprinting,代码行数:25,代码来源:master.py


示例4: generate_bi_graphemes_dictionary

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def generate_bi_graphemes_dictionary(label_list):
    freqs = Counter()
    for label in label_list:
        label = label.split(' ')
        for i in label:
            for pair in split_every(2, i):
                if len(pair) == 2:
                    freqs[pair] += 1
    with open('resources/unicodemap_en_baidu_bi_graphemes.csv', 'w') as bigram_label:
        bigramwriter = csv.writer(bigram_label, delimiter = ',')
        baidu_labels = list('\' abcdefghijklmnopqrstuvwxyz')
        for index, key in enumerate(baidu_labels):
            bigramwriter.writerow((key, index+1))
        for index, key in enumerate(freqs.keys()):
            bigramwriter.writerow((key, index+len(baidu_labels)+1)) 
开发者ID:awslabs,项目名称:dynamic-training-with-apache-mxnet-on-aws,代码行数:19,代码来源:stt_bi_graphemes_util.py


示例5: get_denominator_csv

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def get_denominator_csv(self):
        output = io.StringIO()
        writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)
        writer.writerow(["year", "month", "officers out on service"])
        values = sorted(self.denominator_values,
                        key=lambda x: (x.year, x.month))
        for value in values:
            row = [
                value.year,
                value.month,
                value.officers_out_on_service
            ]
            writer.writerow(row)
        return output.getvalue() 
开发者ID:codeforamerica,项目名称:comport,代码行数:21,代码来源:models.py


示例6: download_results

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def download_results(request):
    import csv
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="orders.csv"'
    writer = csv.writer(response)
    header = [
        'CODE',
        'CUSTOMER',
        'CREATED_AT',
        'ASSIGNED_TO',
        'CHECKED_IN',
        'LOCATION'
    ]
    writer.writerow(header)
    for o in request.session['order_queryset']:
        row = [o.code, o.customer, o.created_at,
               o.user, o.checkin_location, o.location]
        coded = [unicode(s).encode('utf-8') for s in row]
        writer.writerow(coded)
    return response 
开发者ID:fpsw,项目名称:Servo,代码行数:26,代码来源:order.py


示例7: execute

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def execute(self, parameters, messages):
        """The source code of the tool."""
        in_drainage_line = parameters[0].valueAsText
        out_csv_file = parameters[1].valueAsText
        fields = ['NextDownID', 'HydroID']
        list_all = []
        '''The script line below makes sure that rows in the subset file are
           arranged in descending order of NextDownID of stream segements'''
        for row in sorted(arcpy.da.SearchCursor(in_drainage_line, fields), reverse=True):
            list_all.append([row[1]])
        with open(out_csv_file,'wb') as csvfile:
            connectwriter = csv.writer(csvfile, dialect='excel')
            for row_list in list_all:
                out = row_list
                connectwriter.writerow(out)
        return 
开发者ID:Esri,项目名称:python-toolbox-for-rapid,代码行数:23,代码来源:CreateSubsetFile.py


示例8: reset

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def reset(self):
        import csv
        kwargs = {"newline": "", "encoding": "utf-8"}
        mode = "wt"
        if self.complib == "gzip":
            import gzip
            self._fh = gzip.open(self.csvfile, mode, self.complevel, **kwargs)
        elif self.complib in ("bz2", "bzip2"):
            import bz2
            self._fh = bz2.open(self.csvfile, mode, self.complevel, **kwargs)
        elif self.complib is None:
            self._fh = open(self.csvfile, mode, **kwargs)
        else:
            raise KeyError("Unexpected compression library: {}".format(self.complib))
        self._writer = csv.writer(self._fh, **self.csv_kwargs)
        # Write header data
        row = ["Datetime"] + [name for name in self._node_names]
        self._writer.writerow(row) 
开发者ID:pywr,项目名称:pywr,代码行数:21,代码来源:recorders.py


示例9: get_data

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def get_data():
    filenames = [os.path.splitext(f)[0] for f in glob.glob("original/*.jpg")]
    jpg_files = [s + ".jpg" for s in filenames]
    txt_files = [s + ".txt" for s in filenames]
    for file in txt_files:
        boxes = []
        with open(file, "r", encoding="utf-8", newline="") as lines:
            for line in csv.reader(lines):
                boxes.append([line[0], line[1], line[6], line[7]])
        with open('mlt/label/' + file.split('/')[1], "w+") as labelFile:
            wr = csv.writer(labelFile)
            wr.writerows(boxes)
    for jpg in jpg_files:
        shutil.copy(jpg, 'mlt/image/') 
开发者ID:zzzDavid,项目名称:ICDAR-2019-SROIE,代码行数:18,代码来源:prepare_dataset.py


示例10: process_txt

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def process_txt():
    filenames = [os.path.splitext(f)[0] for f in glob.glob("test_result/*.txt")]
    old_files = [s + ".txt" for s in filenames]
    for old_file in old_files:
        new = []
        with open(old_file, "r") as old:
            for line in csv.reader(old):
                if not line:
                    continue
                if not line[0]:
                    continue
                if line[0][0] == ' ' or line[0][-1] == ' ':
                    line[0] = line[0].strip()
                if ' ' in line[0]:
                    line = line[0].split(' ')
                new.append(line)
        with open('task2_result/' + old_file.split('/')[1], "w+") as newfile:
            wr = csv.writer(newfile, delimiter = '\n')
            new = [[s[0].upper()] for s in new]
            wr.writerows(new) 
开发者ID:zzzDavid,项目名称:ICDAR-2019-SROIE,代码行数:22,代码来源:main.py


示例11: for_task3

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def for_task3():
    filenames = [os.path.splitext(f)[0] for f in glob.glob("boundingbox/*.txt")]
    box_files = [s + ".txt" for s in filenames]
    for boxfile in box_files:
        box = []
        with open(boxfile,'r') as boxes:
            for line in csv.reader(boxes):
                box.append([int(string, 10) for string in line[0:8]])
        words = []
        with open('test_result/'+ boxfile.split('/')[1], 'r') as prediction:
            for line in csv.reader(prediction):
                words.append(line)
        words = [s if len(s)!=0 else [' '] for s in words]
        new = []
        for line in zip(box,words):
            a,b = line
            new.append(a+b)
        with open('for_task3/'+ boxfile.split('/')[1], 'w+') as newfile:
            csv_out = csv.writer(newfile)
            for line in new:
                csv_out.writerow(line) 
开发者ID:zzzDavid,项目名称:ICDAR-2019-SROIE,代码行数:23,代码来源:main.py


示例12: write

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def write(self, filename):
        with closing(open(filename + '.new', 'w')) as w:
            c = csv.writer(w)
            c.writerow(['bearing_start','bearing_end','bin_start','bin_end','samples','unique'])
            for b_low,b_high,histo in self.values():
                # make sure we write at least one value per sector,
                # it makes things a little easier when plotting
                first = True
                for h_low,h_high,count,unique in histo.values():
                    if unique or first:
                        c.writerow(['%f' % b_low,
                                    '%f' % b_high,
                                    '%f' % h_low,
                                    '%f' % h_high,
                                    '%d' % count,
                                    '%d' % unique])
                        first = False
        os.rename(filename + '.new', filename) 
开发者ID:mutability,项目名称:dump1090-tools,代码行数:20,代码来源:adsb-polar.py


示例13: outf_writer_compat

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def outf_writer_compat(outfile, encoding, errors, gzip_compress=False):
    """
    Identify appropriate CSV writer given the Python version
    """
    if compat.PY3:
        if gzip_compress:
            outf = gzip.open(outfile, 'wt', encoding=encoding, errors=errors)
        else:
            outf = open(outfile, 'w', encoding=encoding, errors=errors)
        writer = csv.writer(outf)
    else:
        if gzip_compress:
            outf = gzip.open(outfile, 'wb')
        else:
            outf = open(outfile, 'wb')
        writer = compat.UnicodeWriter(outf, encoding=encoding, errors=errors)
    return (writer, outf) 
开发者ID:rafasashi,项目名称:razzy-spinner,代码行数:19,代码来源:common.py


示例14: export

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def export(self, filename, render_func=None):
        if not self.lineups:
            return
        total_players = 0
        with open(filename, 'r') as csvfile:
            lines = list(csv.reader(csvfile))
            for i, lineup in enumerate(self.lineups, start=1):
                if i >= len(lines):
                    lines.append([])
                players_list = [(render_func or self.render_player)(player) for player in lineup.lineup]
                if not total_players:
                    total_players = len(players_list)
                lines[i] = players_list + lines[i][total_players:]
            for line_order in range(i, len(lines) - 1):
                lines[line_order] = [''] * total_players + lines[line_order][total_players:]
        with open(filename, 'w') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerows(lines) 
开发者ID:DimaKudosh,项目名称:pydfs-lineup-optimizer,代码行数:20,代码来源:lineup_exporter.py


示例15: create_csv_file

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def create_csv_file(cls, full_path_file, content, delimiter=','):
        with open(full_path_file, 'w+') as u_file:
            writer = csv.writer(u_file, quoting=csv.QUOTE_NONE, delimiter=delimiter)
            writer.writerows(content) 
开发者ID:apache,项目名称:incubator-spot,代码行数:6,代码来源:utils.py


示例16: _output_table

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def _output_table(output, theader, tdata, left_align):
    if not tdata:
        print("No matching trained models found.", file=sys.stderr)
    if output == 'table':
        if not tdata:
            return
        tdata.insert(0, theader)
        print(DISPLAY.table(tdata, left_align=left_align).getvalue(fit=True))
    elif output == 'json':
        res = []
        for row in tdata:
            res.append(dict(zip(theader, row)))
        print(json.dumps(res))
    elif output == 'csv':
        buffer = io.StringIO()
        writer = csv.writer(buffer)
        writer.writerow(theader)
        for row in tdata:
            writer.writerow(row)
        val = buffer.getvalue()
        val = val.replace('\r', '')
        print(val.strip()) 
开发者ID:mme,项目名称:vergeml,代码行数:29,代码来源:ls.py


示例17: __init__

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def __init__(self, stats, stats_file):
        self.ks = [k['name'] for k in stats if k['log']]
        self.prev = {}
        if not self.ks:
            return
        self.file = open(stats_file, "w", newline='')
        self.writer = csv.writer(self.file)
        self.writer.writerow(["epoch", "step"] + self.ks) 
开发者ID:mme,项目名称:vergeml,代码行数:12,代码来源:env.py


示例18: end

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def end(self):
        if not self.ks:
            return
        self.file.flush()
        self.file.close()
        self.writer = None 
开发者ID:mme,项目名称:vergeml,代码行数:9,代码来源:env.py


示例19: evolution

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def evolution(self, max_eval=100, mutation_rate=0.01, log_file='./log.txt'):
        with open(log_file, 'w') as fw:
            writer = csv.writer(fw, lineterminator='\n')
            eval_flag = np.empty(self.lam)
            self._evaluation([self.pop[0]], np.array([True]))
            print(self._log_data(net_info_type='active_only'))
            while self.num_eval < max_eval:
                self.num_gen += 1
                # reproduction
                for i in range(self.lam):
                    self.pop[i+1].copy(self.pop[0])    # copy a parent
                    eval_flag[i] = self.pop[i+1].mutation(mutation_rate)    # mutation
                # evaluation and selection
                evaluations = self._evaluation(self.pop[1:], eval_flag=eval_flag)
                best_arg = evaluations.argmax()
                if evaluations[best_arg] >= self.pop[0].eval:
                    self.pop[0].copy(self.pop[best_arg+1])
                # display and save log
                if eval_flag.sum() > 0:
                    print(self._log_data(net_info_type='active_only'))
                    writer.writerow(self._log_data(net_info_type='full'))
    # Modified CGP (used for GECCO 2017 paper):
    #   At each iteration:
    #     - Generate lambda individuals in which at least one active node changes (i.e., forced mutation)
    #     - Mutate the best individual with neutral mutation (unchanging the active nodes)
    #         if the best individual is not updated. 
开发者ID:sg-nm,项目名称:cgp-cnn,代码行数:35,代码来源:cgp.py


示例20: modified_evolution

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def modified_evolution(self, max_eval=100, mutation_rate=0.01, log_file='./log.txt'):
        with open(log_file, 'w') as fw:
            writer = csv.writer(fw, lineterminator='\n')
            eval_flag = np.empty(self.lam)
            active_num = self.pop[0].count_active_node()
            while active_num < self.pop[0].net_info.min_active_num or active_num > self.pop[0].net_info.max_active_num:
                self.pop[0].mutation(1.0)
                active_num = self.pop[0].count_active_node()
            self._evaluation([self.pop[0]], np.array([True]))
            print(self._log_data(net_info_type='active_only'))
            while self.num_eval < max_eval:
                self.num_gen += 1
                # reproduction
                for i in range(self.lam):
                    eval_flag[i] = False
                    self.pop[i + 1].copy(self.pop[0])  # copy a parent
                    active_num = self.pop[i + 1].count_active_node()
                    # forced mutation
                    while not eval_flag[i] or active_num < self.pop[i + 1].net_info.min_active_num \
                            or active_num > self.pop[i + 1].net_info.max_active_num:
                        self.pop[i + 1].copy(self.pop[0])  # copy a parent
                        eval_flag[i] = self.pop[i + 1].mutation(mutation_rate)  # mutation
                        active_num = self.pop[i + 1].count_active_node()
                # evaluation and selection
                evaluations = self._evaluation(self.pop[1:], eval_flag=eval_flag)
                best_arg = evaluations.argmax()
                if evaluations[best_arg] > self.pop[0].eval:
                    self.pop[0].copy(self.pop[best_arg + 1])
                else:
                    self.pop[0].neutral_mutation(mutation_rate)  # neutral mutation
                # display and save log
                print(self._log_data(net_info_type='active_only'))
                writer.writerow(self._log_data(net_info_type='full')) 
开发者ID:sg-nm,项目名称:cgp-cnn,代码行数:42,代码来源:cgp.py


示例21: csv

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def csv(self, delimiter=','):
		"""Return data in CSV format"""
		data = self.dict()
		out = StringIO()
		writer = csv.writer(out, delimiter=delimiter, lineterminator='')
		writer.writerow([data['server']['id'], data['server']['sponsor'],
						 data['server']['name'], data['timestamp'],
						 data['server']['d'], data['ping'], data['download'],
						 data['upload']])
		return out.getvalue() 
开发者ID:NatanaelAntonioli,项目名称:L.E.S.M.A,代码行数:13,代码来源:L.E.S.M.A. - Fabrica de Noobs Speedtest.py


示例22: save_dict_to_file

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def save_dict_to_file(filename, dictionary):
  """Saves dictionary as CSV file."""
  with open(filename, 'w') as f:
    writer = csv.writer(f)
    for k, v in iteritems(dictionary):
      writer.writerow([str(k), str(v)]) 
开发者ID:StephanZheng,项目名称:neural-fingerprinting,代码行数:8,代码来源:master.py


示例23: to_csv

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def to_csv(url, path):
    data = fetch_data(url)
    with open(path, 'w', newline='', encoding='utf_8') as csvfile:
        writer = csv.writer(csvfile,
                            delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        writer.writerow(data[0]._fields)
        for d in data:
            writer.writerow([_ for _ in d]) 
开发者ID:mlouielu,项目名称:twstock,代码行数:10,代码来源:fetch.py


示例24: write_output_to_file

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def write_output_to_file(label, sum_counts, append=False):
    print(">> writing output to file '%s'" % output_file)
    fmode = 'a' if append else 'w'
    with open(output_file, fmode + 'b') as f:
        writer = csv.writer(f)
        for noun, count in sum_counts:
            writer.writerow([label.encode('utf-8'), noun.encode('utf-8'), count])
    print(">> done") 
开发者ID:internaut,项目名称:facebook-discussion-tk,代码行数:12,代码来源:analyze_noun_counts.py


示例25: write_csv

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def write_csv(log_loc, args):
    for net in args.networks:
        with open(log_loc + '/' + net.name + '.csv', 'wb') as f:
            w = csv.writer(f)
            w.writerow(['num_gpus', 'img_processed_per_sec'])
            w.writerows(net.gpu_speedup.items()) 
开发者ID:awslabs,项目名称:dynamic-training-with-apache-mxnet-on-aws,代码行数:8,代码来源:benchmark.py


示例26: get_uof_csv

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def get_uof_csv(self):
        output = io.StringIO()
        writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)
        uof_class = getattr(importlib.import_module("comport.data.models"), "UseOfForceIncident{}".format(self.short_name))
        csv_schema = uof_class.get_csv_schema()
        csv_headers = [col[0] for col in csv_schema]
        csv_vars = [col[1] for col in csv_schema]
        writer.writerow(csv_headers)
        use_of_force_incidents = uof_class.query.all()
        for incident in use_of_force_incidents:
            values = []
            for incident_var in csv_vars:
                incident_value = getattr(incident, incident_var)
                if incident_var.endswith("_date"):
                    incident_value = coalesce_date(incident_value)
                values.append(incident_value)
            writer.writerow(values)
        return output.getvalue() 
开发者ID:codeforamerica,项目名称:comport,代码行数:28,代码来源:models.py


示例27: get_ois_csv

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def get_ois_csv(self):
        output = io.StringIO()
        writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)
        ois_class = getattr(importlib.import_module("comport.data.models"), "OfficerInvolvedShooting{}".format(self.short_name))
        csv_schema = ois_class.get_csv_schema()
        csv_headers = [col[0] for col in csv_schema]
        csv_vars = [col[1] for col in csv_schema]
        writer.writerow(csv_headers)
        officer_involved_shootings = ois_class.query.all()
        for incident in officer_involved_shootings:
            values = []
            for incident_var in csv_vars:
                incident_value = getattr(incident, incident_var)
                if incident_var.endswith("_date"):
                    incident_value = coalesce_date(incident_value)
                values.append(incident_value)
            writer.writerow(values)
        return output.getvalue() 
开发者ID:codeforamerica,项目名称:comport,代码行数:28,代码来源:models.py


示例28: get_complaint_csv

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def get_complaint_csv(self):
        output = io.StringIO()
        writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)
        complaint_class = getattr(importlib.import_module("comport.data.models"), "CitizenComplaint{}".format(self.short_name))
        csv_schema = complaint_class.get_csv_schema()
        csv_headers = [col[0] for col in csv_schema]
        csv_vars = [col[1] for col in csv_schema]
        writer.writerow(csv_headers)
        complaints = complaint_class.query.all()
        for complaint in complaints:
            values = []
            for incident_var in csv_vars:
                incident_value = getattr(complaint, incident_var)
                if incident_var.endswith("_date"):
                    incident_value = coalesce_date(incident_value)
                values.append(incident_value)
            writer.writerow(values)
        return output.getvalue() 
开发者ID:codeforamerica,项目名称:comport,代码行数:28,代码来源:models.py


示例29: get_pursuits_csv

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def get_pursuits_csv(self):
        output = io.StringIO()
        writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)
        pursuits_class = getattr(importlib.import_module("comport.data.models"), "Pursuit{}".format(self.short_name))
        csv_schema = pursuits_class.get_csv_schema()
        csv_headers = [col[0] for col in csv_schema]
        csv_vars = [col[1] for col in csv_schema]
        writer.writerow(csv_headers)
        incidents = pursuits_class.query.all()
        for incident in incidents:
            values = []
            for incident_var in csv_vars:
                incident_value = getattr(incident, incident_var)
                if incident_var.endswith("_date"):
                    incident_value = coalesce_date(incident_value)
                values.append(incident_value)
            writer.writerow(values)
        return output.getvalue() 
开发者ID:codeforamerica,项目名称:comport,代码行数:28,代码来源:models.py


示例30: get_assaults_csv

# 需要导入模块: import csv [as 别名]
# 或者: from csv import writer [as 别名]
def get_assaults_csv(self):
        output = io.StringIO()
        writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)
        assaults_class = getattr(importlib.import_module("comport.data.models"), "AssaultOnOfficer{}".format(self.short_name))
        csv_schema = assaults_class.get_csv_schema()
        csv_headers = [col[0] for col in csv_schema]
        csv_vars = [col[1] for col in csv_schema]
        writer.writerow(csv_headers)
        incidents = assaults_class.query.all()
        for incident in incidents:
            values = []
            for incident_var in csv_vars:
                incident_value = getattr(incident, incident_var)
                if incident_var.endswith("_date"):
                    incident_value = coalesce_date(incident_value)
                values.append(incident_value)
            writer.writerow(values)
        return output.getvalue() 
开发者ID:codeforamerica,项目名称:comport,代码行数:28,代码来源:models.py



注:本文中的csv.writer方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。