Hi and thanks, Mark, Ned and Graeme.
the .mrk file as output was definitely what I was going for, Graeme... your code works perfectly.
Thanks for your help - and I have much to learn!
From: Code for Libraries <[log in to unmask]> on behalf of Graeme Williams <[log in to unmask]>
Sent: Thursday, May 14, 2020 4:55 PM
To: [log in to unmask]
Subject: Re: [CODE4LIB] PyMARC question
Do you want to generate an .mrk file as output? The following code will
generate a .mrk file with an output record for every input record, but the
output records will only have 035 fields.
from pymarc import Record, MARCReader
'rb') as marc_input:
for input_record in MARCReader(marc_input):
the_fields = input_record.get_fields('035')
new_record = Record()
This will output just a leader for any input records that don't have 035s.
If you wanted to skip those records you'd have to check the_fields against
If this *isn't* the output format you wanted, perhaps you can give us an
example of what you are looking for?
Las Vegas, NV
On Thu, May 14, 2020 at 1:41 PM Ned Stewart <[log in to unmask]> wrote:
> Hi Richard,
> I'm with Mark in thinking that you don't need the MARCWriter for the
> second part.
> Also, I think I'd do another for loop, like in the first part, after
> opening the file:
> for fld in ARN:
> ARNfile.write(fld + '\n')
> And then close the file. Pretty sure you need to add your own line feed,
> thus the fld + '\n'
> Hope this helps a little as well,
> -----Original Message-----
> From: Code for Libraries <[log in to unmask]> On Behalf Of Mark A.
> Sent: Thursday, May 14, 2020 3:47 PM
> To: [log in to unmask]
> Subject: Re: [CODE4LIB] PyMARC question
> Hi Richard,
> It seems like your code in the first part might be working as expected -
> does it print the 035 fields as you expect?
> PyMARC's `get_fields()` function will return either a Python list of Field
> objects, or an empty list (``) if there are no matching fields for the
> given tag. This leads to the case where `ARNfile.write(ARN)` won't work,
> because you have to cast the Python list to a string first.
> Also, it seems like you probably don't need to import the `MARCWriter`
> class if I'm understanding your needs - that is for writing files of MARC
> records themselves.
> Hope that helps,
> Mark A. Matienzo
> Unceded Ohlone Territory / San Mateo, California
> On Thu, May 14, 2020 at 12:22 PM Richard Guinn <[log in to unmask]>
> > Hello
> > Apologies for a very basic question - but I am running into a wall
> > with some code.
> > I am very new to Python and PyMARC.
> > I am trying to use PyMARC to pull a list of 035's from a list of
> > MARC21 records. I want to have the list of 035's to output to a text
> > My code thus far:
> > # List all OCLC numbers in the file
> > import pymarc
> > from pymarc import MARCReader
> > with
> > open(r'C:\Users\RickRigby\Desktop\RichardLGuinninterests\metadataMARC.
> > mrc','rb')
> > as fh:
> > reader = MARCReader(fh)
> > for record in reader:
> > print(record['035'])
> > from pymarc import MARCWriter
> > ARN = record.get_fields('035')
> > ARNfile =
> > open(r'C:\Users\RickRigby\Desktop\RichardLGuinninterests\metadata.mrk'
> > ,'w')
> > ARNfile.write(str(ARN))
> > ARNfile.close()?
> > it creates a text file but with nothing in the file.
> > How can I get this to work?
> > Also - I would like a list in the text file but when I use just
> > ARNfile.write(ARN)
> > I get a TypeError: TypeError: write() argument must be str, not list
> > I'm not sure if there's a way to get around that...
> > Thank you very much for any help!
> > Richard