def mrc_to_mrk(path_in, path_out): reader = pymarc.MARCReader(open(path_in, 'rb'), to_unicode=True, force_utf8=True) writer = pymarc.TextWriter(io.open(path_out, 'wt', encoding="UTF-8")) for record in reader: writer.write(record) writer.close()
def test_close_true(self): """If close_fh is true, then the file handle is also closed.""" file_handle = StringIO() self.assertFalse(file_handle.closed, "The file handle should be open") writer = pymarc.TextWriter(file_handle) self.assertFalse(file_handle.closed, "The file handle should still be open") writer.close() self.assertTrue(file_handle.closed, "The file handle should close when the writer closes")
def test_writing_0_records(self): file_handle = StringIO() try: writer = pymarc.TextWriter(file_handle) writer.close(close_fh=False) self.assertEqual( file_handle.getvalue(), '', 'Nothing should be have been written to the file handle') finally: file_handle.close()
def test_close_false(self): """If close_fh is false, then the file handle is NOT closed.""" file_handle = StringIO() self.assertFalse(file_handle.closed, "The file handle should be open") writer = pymarc.TextWriter(file_handle) self.assertFalse(file_handle.closed, "The file handle should still be open") writer.close(close_fh=False) self.assertFalse( file_handle.closed, "The file handle should NOT close when the writer closes", )
def test_writing_empty_record(self): expected = r""" =LDR 22 4500 """ expected = textwrap.dedent(expected[1:]) file_handle = StringIO() try: writer = pymarc.TextWriter(file_handle) record = pymarc.Record() writer.write(record) writer.close(close_fh=False) self.assertEquals(file_handle.getvalue(), expected) finally: file_handle.close()
def test_writing_3_records(self): expected = r""" =LDR 22 4500 =008 090227s2009\\\\mau\\\\\\\\\\\\\\\\\chi\d =100 00$ame =245 00$aFoo /$cby me. =LDR 22 4500 =100 00$ame =245 00$aFoo /$cby me. =LDR 22 4500 =245 00$aFoo /$cby me. """ expected = textwrap.dedent(expected[1:]) file_handle = StringIO() try: writer = pymarc.TextWriter(file_handle) record = pymarc.Record() record.add_field( pymarc.Field( '008', data=u('090227s2009 mau chi d'))) record.add_field(pymarc.Field('100', ['0', '0'], ['a', u('me')])) record.add_field( pymarc.Field( '245', ['0', '0'], ['a', u('Foo /'), 'c', u('by me.')])) writer.write(record) record = pymarc.Record() record.add_field(pymarc.Field('100', ['0', '0'], ['a', u('me')])) record.add_field( pymarc.Field( '245', ['0', '0'], ['a', u('Foo /'), 'c', u('by me.')])) writer.write(record) record = pymarc.Record() record.add_field( pymarc.Field( '245', ['0', '0'], ['a', u('Foo /'), 'c', u('by me.')])) writer.write(record) writer.close(close_fh=False) self.assertEquals(file_handle.getvalue(), expected) finally: file_handle.close()
def test_writing_1_record(self): expected = r""" =LDR 22 4500 =100 00$ame =245 00$aFoo /$cby me. """ expected = textwrap.dedent(expected[1:]) file_handle = StringIO() try: writer = pymarc.TextWriter(file_handle) record = pymarc.Record() record.add_field(pymarc.Field("100", ["0", "0"], ["a", "me"])) record.add_field( pymarc.Field("245", ["0", "0"], ["a", "Foo /", "c", "by me."])) writer.write(record) writer.close(close_fh=False) self.assertEquals(file_handle.getvalue(), expected) finally: file_handle.close()
def write_to_file(reclist, filename="output", form="bin"): """write records to file""" if form == "bin": filename = filename + ".mrc" with open(filename, "wb") as out: for record in reclist: out.write(record.as_marc()) elif form == "xml": filename = filename + ".xml" writer = pymarc.XMLWriter(open(filename, "wb")) for record in reclist: writer.write(record) writer.close() elif form == "text": filename = filename + ".txt" with open(filename, "wt", encoding="utf-8") as out: writer = pymarc.TextWriter(out) for record in reclist: writer.write(record)
def xml_to_mrk(path_in, path_out): writer = pymarc.TextWriter(io.open(path_out, 'wt', encoding="utf-8")) records = pymarc.map_xml(writer.write, path_in) writer.close()