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,
Ned
-----Original Message-----
From: Code for Libraries <[log in to unmask]> On Behalf Of Mark A. Matienzo
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
--
Mark A. Matienzo
Unceded Ohlone Territory / San Mateo, California
https://nam03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmatienzo.org%2F&data=02%7C01%7Cnstewart%40FLVC.ORG%7C52794ea2336a4ae6cdfb08d7f83fb188%7C60ebd441a2f94841802f22bf1380b4ae%7C0%7C0%7C637250824752827635&sdata=3d%2BvJIL7SpxlyM7dNWvJJPFv9l%2F8G%2BPDSYBixjAS3FA%3D&reserved=0
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
>
|