Python csv.DictWriter() Examples

The following are 30 code examples of csv.DictWriter(). 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: tools.py    From iqfeed with Apache License 2.0 7 votes vote down vote up
def write_bars_to_file(bars, filename, tz):
    """Creates CSV file from list of Bar instances"""
    date_format_str = "%Y%m%d %H%M%S"

    rows = [{'DateTime':  bar.datetime.astimezone(tz).strftime(date_format_str),
             'Open':	  bar.open,
             'High':	  bar.high,
             'Low':	      bar.low,
             'Close':	  bar.close,
             'Volume':	  bar.volume,
             } for bar in bars]

    if os.path.exists(filename):
        raise Exception("File already exists!")

    fd = os.popen("gzip > %s" % filename, 'w') if filename.endswith('.gz') else open(filename, 'w')

    with fd:
        csv_writer = csv.DictWriter(fd, ['DateTime', 'Open', 'High', 'Low', 'Close', 'Volume'])
        csv_writer.writeheader()
        csv_writer.writerows(rows) 
Example #2
Source File: test_csv.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_write_fields_not_in_fieldnames(self):
        fd, name = tempfile.mkstemp()
        fileobj = os.fdopen(fd, "w+b")
        try:
            writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
            # Of special note is the non-string key (issue 19449)
            with self.assertRaises(ValueError) as cx:
                writer.writerow({"f4": 10, "f2": "spam", 1: "abc"})
            exception = str(cx.exception)
            self.assertIn("fieldnames", exception)
            self.assertIn("'f4'", exception)
            self.assertNotIn("'f2'", exception)
            self.assertIn("1", exception)
        finally:
            fileobj.close()
            os.unlink(name) 
Example #3
Source File: put_repo_requests_in_db.py    From oadoi with MIT License 6 votes vote down vote up
def save_repo_request_rows(rows):

    with open('out.csv','wb') as f:

        w = csv.DictWriter(f, fieldnames=RepoRequest.list_fieldnames(), encoding='utf-8-sig')

        for row in rows[1:]:  # skip header row
            my_repo_request = RepoRequest()
            my_repo_request.set_id_seed(row[0])
            column_num = 0
            for fieldname in RepoRequest.list_fieldnames():
                if fieldname != "id":
                    setattr(my_repo_request, fieldname, row[column_num])
                    column_num += 1

            w.writerow(my_repo_request.to_dict())
            print u"adding repo request {}".format(my_repo_request)
            db.session.merge(my_repo_request)

        safe_commit(db) 
Example #4
Source File: report.py    From ur-analysis-tools with Apache License 2.0 6 votes vote down vote up
def report(self, column_names, columns, cfg=None, **kwargs):
        formated_columns = []
        for column in columns:
            formated_column = []
            for value in column:
                if isinstance(value, int):
                    formated_column.append(value)
                    #formated_column.append("{:,}".format(value))
                elif isinstance(value, float):
                    formated_column.append("%.4f" % value)
                else:
                    formated_column.append(value)
            formated_columns.append(formated_column)

        writer = csv.DictWriter(self.fd, fieldnames=column_names)
        writer.writeheader()

        for i in range(len(formated_columns[0])):
            row = dict([(column_names[j], formated_columns[j][i])
                for j in range(len(formated_columns))])
            writer.writerow(row)

        self.report_config(cfg) 
Example #5
Source File: get_ec2_data.py    From aws-cost-report with MIT License 6 votes vote down vote up
def write_reservation_usage(f, reservation_usage, header=True):
    writer = csv.DictWriter(f, fieldnames=[
        'instance_type',
        'availability_zone',
        'tenancy',
        'product',
        'cost_hourly',
        'cost_upfront',
        'count',
        'count_used',
    ])
    if header:
        writer.writeheader()
    for ru in reservation_usage:
        writer.writerow({
            'instance_type': ru.instance_reservation.type.size,
            'availability_zone': ru.instance_reservation.type.availability_zone,
            'tenancy': ru.instance_reservation.type.tenancy,
            'product': ru.instance_reservation.type.product,
            'cost_hourly': ru.instance_reservation.cost_hourly,
            'cost_upfront': ru.instance_reservation.cost_upfront,
            'count': ru.count,
            'count_used': ru.count_used,
        }) 
Example #6
Source File: getLog.py    From psychsim with MIT License 5 votes vote down vote up
def readLog(gameID,cursor):
    for player in range(4):
        # Read in-game survey data from db
        cursor.execute('SELECT * FROM sim_in_game_survey WHERE '+
                       'sigs_game_id=%d ' % (gameID)+
                       'AND sigs_player_number=%d' % (player+1))
        results = cursor.fetchall()
        if results:
            fields = sorted(results[0].keys())
            with open('%d_%d_survey_db.csv' % (gameID,player+1),'w') as csvfile:
                writer = csv.DictWriter(csvfile,fields,extrasaction='ignore')
                writer.writeheader()
                for record in results:
                    writer.writerow(record)
        # Read game log data from db
        cursor.execute('SELECT spl_message FROM sim_project_log WHERE '+
                       'spl_game_id=%d ' % (gameID)+
                       'AND spl_player_number=%d' % (player+1))
        results = cursor.fetchall()
        if results:
            fields = sorted(results[0].keys())
            with open('%d_%d_logs.csv' % (gameID,player+1),'w') as csvfile:
                writer = csv.DictWriter(csvfile,fields,extrasaction='ignore')
                writer.writeheader()
                for record in results:
                    writer.writerow(record) 
Example #7
Source File: util.py    From DeepLearn with MIT License 5 votes vote down vote up
def save_predictions(pred, file):

    """
    Save predictions to CSV file
    Args:
        pred: numpy array, of numeric predictions
        file: str, filename + extension
    """

    with open(file, 'w') as csvfile:
        fieldnames = ['Stance']
        writer = DictWriter(csvfile, fieldnames=fieldnames)

        writer.writeheader()
        for instance in pred:
            writer.writerow({'Stance': label_ref_rev[instance]}) 
Example #8
Source File: records.py    From csvdiff with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def save(records: Sequence[Record], fieldnames: List[Column], ostream: TextIO):
    writer = csv.DictWriter(ostream, fieldnames)
    writer.writeheader()
    for r in records:
        writer.writerow(r) 
Example #9
Source File: test_csvdiff.py    From csvdiff with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def save_as_csv(records, filename):
    with open(filename, 'w') as ostream:
        header = sorted(records[0].keys())

        writer = csv.DictWriter(ostream, header)

        writer.writerow(dict(zip(header, header)))
        for record in records:
            writer.writerow(record) 
Example #10
Source File: capture.py    From meddle with MIT License 5 votes vote down vote up
def __init__(self, filename, headers):
		self.file = open(filename, "wb")
		self.writer = csv.DictWriter(self.file, headers)
		self.lock = Lock() 
Example #11
Source File: test_csv.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_write_no_fields(self):
        fileobj = StringIO()
        self.assertRaises(TypeError, csv.DictWriter, fileobj) 
Example #12
Source File: test_csv.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_write_simple_dict(self):
        fd, name = tempfile.mkstemp()
        fileobj = io.open(fd, 'w+b')
        try:
            writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
            writer.writeheader()
            fileobj.seek(0)
            self.assertEqual(fileobj.readline(), "f1,f2,f3\r\n")
            writer.writerow({"f1": 10, "f3": "abc"})
            fileobj.seek(0)
            fileobj.readline() # header
            self.assertEqual(fileobj.read(), "10,,abc\r\n")
        finally:
            fileobj.close()
            os.unlink(name) 
Example #13
Source File: Util.py    From QTS_Research with MIT License 5 votes vote down vote up
def dict_array_to_csv(dict_list,filename,fields=None):
    mode = 'a' if os.path.exists(filename) else 'w'
    with open(filename, mode, newline='') as csvfile:
        if fields == None:
            fields = dict_list[0].keys()
        writer = csv.DictWriter(csvfile, fieldnames=fields)
        writer.writeheader()
        for d in dict_list:
            writer.writerow(d) 
Example #14
Source File: get_ec2_data.py    From aws-cost-report with MIT License 5 votes vote down vote up
def write_matched_instances(f, matched_instances, header=True):
    writer = csv.DictWriter(f, fieldnames=[
        'instance_type',
        'availability_zone',
        'tenancy',
        'product',
        'cost_ondemand',
        'cost_reserved_worst',
        'cost_reserved_best',
        'count',
        'count_reserved',
    ])
    if header:
        writer.writeheader()
    for mi in matched_instances:
        writer.writerow({
            'instance_type': mi.instance_offering.type.size,
            'availability_zone': mi.instance_offering.type.availability_zone,
            'tenancy': mi.instance_offering.type.tenancy,
            'product': mi.instance_offering.type.product,
            'cost_ondemand': mi.instance_offering.cost_ondemand,
            'cost_reserved_worst': mi.instance_offering.cost_reserved_worst,
            'cost_reserved_best': mi.instance_offering.cost_reserved_best,
            'count': mi.count,
            'count_reserved': mi.count_reserved,
        }) 
Example #15
Source File: get_ec2_metadata.py    From aws-cost-report with MIT License 5 votes vote down vote up
def write_instances_metadata(f, reservation_usage):
    writer = csv.DictWriter(f, fieldnames=[
        'instance_id',
        'name',
        'ebs',
    ])
    writer.writeheader()
    for m in metadata:
        writer.writerow(m) 
Example #16
Source File: file_formats.py    From dataflows with MIT License 5 votes vote down vote up
def __init__(self, file, schema, use_titles=False, **options):
        headers = [f.name for f in schema.fields]
        if use_titles:
            titles = [f.descriptor.get('title', f.name) for f in schema.fields]
            csv_writer = CsvTitlesDictWriter(file, headers, fieldtitles=titles)
        else:
            csv_writer = csv.DictWriter(file, headers)
        csv_writer.writeheader()
        super(CSVFormat, self).__init__(csv_writer, schema, **options) 
Example #17
Source File: callbacks.py    From lambda-packs with MIT License 5 votes vote down vote up
def on_epoch_end(self, epoch, logs=None):
    logs = logs or {}

    def handle_value(k):
      is_zero_dim_ndarray = isinstance(k, np.ndarray) and k.ndim == 0
      if isinstance(k, Iterable) and not is_zero_dim_ndarray:
        return '"[%s]"' % (', '.join(map(str, k)))
      else:
        return k

    if not self.writer:
      self.keys = sorted(logs.keys())

      class CustomDialect(csv.excel):
        delimiter = self.sep

      self.writer = csv.DictWriter(
          self.csv_file,
          fieldnames=['epoch'] + self.keys,
          dialect=CustomDialect)
      if self.append_header:
        self.writer.writeheader()

    row_dict = OrderedDict({'epoch': epoch})
    row_dict.update((key, handle_value(logs[key])) for key in self.keys)
    self.writer.writerow(row_dict)
    self.csv_file.flush() 
Example #18
Source File: __main__.py    From ssim with GNU General Public License v3.0 5 votes vote down vote up
def main():

    records = ssim.read(input_file)
    flights = ssim.expand_slots(records)

    keys = flights[0].keys()

    with open(output_file, "w") as csvfile:
        dict_writer = csv.DictWriter(csvfile, keys, restval="")
        dict_writer.writeheader()
        dict_writer.writerows(flights) 
Example #19
Source File: modeling.py    From psychsim with MIT License 5 votes vote down vote up
def readInputData(self,fname=None):
        if fname is None:
            fname = '%s-input.csv' % (self.filename)
        if os.path.isfile(fname):
            self.data = self.readDataFile(fname)
        else:
            raw = self.readDataFile('%s-raw.csv' % (self.filename))
            self.processData(raw)
            fields = sorted(next(iter(self.data.values())).keys())
            for row in self.data.values():
                assert set(row.keys()) == set(fields)
            with open(fname,'w') as csvfile:
                writer = csv.DictWriter(csvfile,fields,extrasaction='ignore')
                writer.writeheader()
                for ID,record in sorted(self.data.items()):
                    writer.writerow(record) 
Example #20
Source File: modeling.py    From psychsim with MIT License 5 votes vote down vote up
def writePredictions(self,fname=None):
        if fname is None:
            fname = '%s-predictions.csv' % (self.filename)
        with open(filename,'w') as csvfile:
            fields = None
            for ID,record in sorted(self.data.items()):
                for field in sorted(self.targets): 
                    for model,prediction in sorted(record['__prediction__'][field].items()):
                        newRecord = {field: record[field] for field in self.idFields}
                        newRecord['__target__'] = field
                        newRecord[field] = prediction
                        try:
                            newRecord['P(%s)' % (field)] = record['__probability__'][field][model]
                        except KeyError:
                            newRecord['P(%s)' % (field)] = ''
                        links = self.model2links(model)
                        for variation in self.variations:
                            code = variation['code']
                            value = variation['domain'][links[code]]
                            if value is None:
                                newRecord[code] = ''
                            elif len(value) == 1:
                                newRecord[code] = '%d' % (value[0])
                            else:
                                assert len(value) == 2
                                newRecord[code] = '%d:%d' % tuple(value)
                        if fields is None:
                            fields = sorted(newRecord.keys())
                            writer = csv.DictWriter(csvfile,fields,extrasaction='ignore')
                            writer.writeheader()
                        writer.writerow(newRecord) 
Example #21
Source File: reporter.py    From appmetrics with Apache License 2.0 5 votes vote down vote up
def dump_meter(self, name, obj):

        # we already know its kind
        kind = obj.pop('kind')

        # add the current time
        obj['time'] = time.time()

        file_name, new = self.file_name(name, kind)

        with open(file_name, "a" if py3comp.PY3 else "ab") as of:
            writer = csv.DictWriter(of, self.meter_header)

            # if the file is new, write the header once
            if new:
                writer.writerow(dict(zip(self.meter_header, self.meter_header)))

            writer.writerow(obj)

        return file_name 
Example #22
Source File: reporter.py    From appmetrics with Apache License 2.0 5 votes vote down vote up
def dump_histogram(self, name, obj):

        # histogram doesn't fit into a tabular format
        obj.pop('histogram')

        # we already know its kind
        kind = obj.pop('kind')

        # flatten percentiles
        percentiles = obj.pop('percentile')
        for k, v in percentiles:
            obj['percentile_{}'.format(k)] = v

        # add the current time
        obj['time'] = time.time()

        file_name, new = self.file_name(name, kind)

        with open(file_name, "a" if py3comp.PY3 else "ab") as of:
            writer = csv.DictWriter(of, self.histogram_header)

            # if the file is new, write the header once
            if new:
                writer.writerow(dict(zip(self.histogram_header, self.histogram_header)))

            writer.writerow(obj)

        return file_name 
Example #23
Source File: callbacks.py    From astroNN with MIT License 5 votes vote down vote up
def savefile(self, folder_name=None):
        """
        the method to actually save the file to disk

        :param folder_name: foldername, can be None to save to current directory
        :type folder_name: Union[NoneType, str]
        """
        if folder_name is not None:
            full_path = os.path.normpath(os.path.join(os.getcwd(), folder_name))
            if not os.path.exists(full_path):
                os.makedirs(full_path)
            self.filename = os.path.join(full_path, self.filename)

        if self.append:
            if os.path.exists(self.filename):
                with open(self.filename, 'r') as f:
                    self.append_header = not bool(len(f.readline()))
            self.csv_file = open(self.filename, 'a')
        else:
            self.csv_file = open(self.filename, 'w')

        class CustomDialect(csv.excel):
            delimiter = self.sep

        self.keys = sorted(self.history.keys())

        self.writer = csv.DictWriter(self.csv_file, fieldnames=['epoch'] + self.keys, dialect=CustomDialect)
        if self.append_header:
            self.writer.writeheader()

        for i in self.epoch:
            self.writer.writerow({**{'epoch': self.epoch[i]}, **dict([(k, self.history[k][i]) for k in self.keys])})
        self.csv_file.close() 
Example #24
Source File: forensic_artifacts.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def _write_csv_file(self, out_fd, result):
        fieldnames = [x["name"] for x in result.fields]
        writer = csv.DictWriter(
            out_fd, dialect="excel",
            fieldnames=fieldnames)
        writer.writeheader()
        for row in result.results:
            writer.writerow(row) 
Example #25
Source File: forensic_artifacts.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def _write_csv_file(self, out_fd, result):
        fieldnames = [x["name"] for x in result.fields]
        writer = csv.DictWriter(
            out_fd, dialect="excel",
            fieldnames=fieldnames)
        writer.writeheader()
        for row in result.results:
            writer.writerow(row) 
Example #26
Source File: petition.py    From petitions with MIT License 5 votes vote down vote up
def generate_modified_file(src, dst, sample, corrupt):
    """원본 파일을 샘플링하고 결측치 넣은 새 파일 생성"""

    # 랜덤 시드 고정. 매번 동일한 결과가 보장되도록.
    random.seed(0)
    with open(src, 'r') as fr:
        with open(dst, 'w') as fw:
            csvr = csv.DictReader(fr)
            csvw = csv.DictWriter(fw, csvr.fieldnames)

            csvw.writeheader()

            rows = csvr

            # 샘플링
            if sample:
                rows = (row for row in rows if random.random() <= SAMPLE_RATE)
            # 결측치 추가
            if corrupt:
                rows = (corrupt_row(row) for row in rows)

            csvw.writerows(rows) 
Example #27
Source File: pricerecorder.py    From flumine with MIT License 5 votes vote down vote up
def process_market_book(self, live_market, market_book):
        with open(self.file_directory, "a") as f:
            writer = csv.DictWriter(f, fieldnames=HEADERS)
            for runner in market_book.runners:
                writer.writerow(
                    {
                        "market_id": market_book.market_id,
                        "publish_time": market_book.publish_time,
                        "status": market_book.status,
                        "inplay": market_book.inplay,
                        "selection_id": runner.selection_id,
                        "last_price_traded": runner.last_price_traded,
                        "back": get_price(runner.ex.available_to_back, 0),
                        "lay": get_price(runner.ex.available_to_lay, 0),
                    }
                ) 
Example #28
Source File: pricerecorder.py    From flumine with MIT License 5 votes vote down vote up
def add(self) -> None:
        # check local dir
        if not os.path.isdir(self.local_dir):
            raise OSError("File dir %s does not exist" % self.local_dir)
        # write headers
        with open(self.file_directory, "w") as f:
            writer = csv.DictWriter(f, fieldnames=HEADERS)
            writer.writeheader() 
Example #29
Source File: mixins.py    From ideascube with GNU Affero General Public License v3.0 5 votes vote down vote up
def to_csv(self):
        out = StringIO()
        headers = self.get_headers()
        writer = csv.DictWriter(out, headers)
        writer.writeheader()
        for item in self.get_items():
            row = self.get_row(item)
            writer.writerow(row)
        out.seek(0)
        return out.read() 
Example #30
Source File: monitor.py    From lirpg with MIT License 5 votes vote down vote up
def __init__(self, env, filename, allow_early_resets=False, reset_keywords=(), info_keywords=()):
        Wrapper.__init__(self, env=env)
        self.tstart = time.time()
        if filename is None:
            self.f = None
            self.logger = None
        else:
            if not filename.endswith(Monitor.EXT):
                if osp.isdir(filename):
                    filename = osp.join(filename, Monitor.EXT)
                else:
                    filename = filename + "." + Monitor.EXT
            self.f = open(filename, "wt")
            self.f.write('#%s\n'%json.dumps({"t_start": self.tstart, 'env_id' : env.spec and env.spec.id}))
            self.logger = csv.DictWriter(self.f, fieldnames=('r', 'l', 't')+reset_keywords+info_keywords)
            self.logger.writeheader()
            self.f.flush()

        self.reset_keywords = reset_keywords
        self.info_keywords = info_keywords
        self.allow_early_resets = allow_early_resets
        self.rewards = None
        self.needs_reset = True
        self.episode_rewards = []
        self.episode_lengths = []
        self.episode_times = []
        self.total_steps = 0
        self.current_reset_info = {} # extra info about the current episode, that was passed in during reset()