Print

Print


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

--
Mark A. Matienzo
Unceded Ohlone Territory / San Mateo, California
https://matienzo.org/


On Thu, May 14, 2020 at 12:22 PM Richard Guinn <[log in to unmask]>
wrote:

> 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 file.
>
>
> 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
>