MMpy.filetools.Writer.write_row

Syntax

MMpy.filetools.Writer.write_row(row_dict, row_id=None)

Description

Write a single row to a data file.

Parameters

Name
Type
Description

row_dict

dict

A dictionary of field-name and value pairs to write to the

row_id

int (optional)

The row number of the file to write values to. If not provided, an internal row counter will be incremented from the first row.

Notes

Care should be taken when using this method as fields will be overwritten without warning.

A ValueError is raised if row_id is less than 1 or greater than the number of rows.

Keys in row_dict that are not valid field names in the data file will be ignored. These must be added to the file structure prior to calling this method if their values should be written to the file.

See Also

MMpy.filetools.Writer()

MMpy.filetools.write_all()

Examples

import MMpy
from MMpy.filetools import ContextManager, RowIterator, Writer

# Create the file we'll write to
proj_path = MMpy.Project().path()
MMpy.File.copy(proj_path + 'source_file.DAT', proj_path + 'dest_file.DAT')

# Iterate over the source, process data, and write changes to the destination.
with ContextManager('dest_file.DAT') as dest:
  writer = Writer(dest)
  with ContextManager('source_file.DAT') as src:
    for row in RowIterator(src):
      # process row data
      writer.write_row(row)
  first_row = next(RowIterator(src))
  last_pos = len(dest)
  writer.write_row(row, last_pos) # overwrites the last row with the first

Resource ID

IDPH_FILETOOLS_WRITER_WRITE_ROW