Editing last subfield

37 views
Skip to first unread message

Chelsea Patella

unread,
Nov 15, 2023, 10:51:47 AM11/15/23
to pymarc Discussion
Hi all,

I've updated to Pymarc v.5 and see that one of my processes doesn't work anymore. I used to be able to edit the last subfield of a field using something like the code below (removes a period at the end of a 245 field). 

record['245'].subfields[-1] = re.sub('\.$', '', record['245'].subfields[-1])

Now I get the error message: TypeError: expected string or bytes-like object

It doesn't work when I use record['245'].subfields[-1].value either. Can anyone tell me how I can edit the punctuation at the end of a field (without knowing the last subfield) in Pymarc v.5?

Thank you!

Ed Summers

unread,
Nov 15, 2023, 11:22:29 AM11/15/23
to pym...@googlegroups.com
Hi Chelsea,

> On Nov 15, 2023, at 10:51 AM, 'Chelsea Patella' via pymarc Discussion <pym...@googlegroups.com> wrote:
>
> record['245'].subfields[-1] = re.sub('\.$', '', record['245'].subfields[-1])

Sadly the subfields are now NamedTuples, which are immutable. So this will give you a "AttributeError: can't set attribute” error:

record['245'].subfields[-1].value = re.sub('\.$', '', record['245'].subfields[-1].value)

Maybe someone has a better solution here, but one (tedious) way of approaching this might be to create a variable for the last subfield, and then use it to construct a new subfield:

sf = record['245’].subfields[-1]
record['245'].subfields[-1] = Subfield(sf.code, re.sub('\.$', '', sf.value)

I think using the placeholder variable makes it a bit more readable than this (which looks abysmal):

record['245'].subfields[-1] = Subfield(
record['245'].subfields[-1].code,
re.sub('\.$', '', records['245'].subfields[-1].value
)

//Ed

Chelsea Patella

unread,
Nov 15, 2023, 12:56:46 PM11/15/23
to pym...@googlegroups.com
That solved it - thank you!

Chelsea E. Patella, MLIS
Library IT Specialist / Python Developer
Cassidy Cataloguing Services, Inc.
248 W. Main St., Suite 2
Rockaway, NJ 07866


--
You received this message because you are subscribed to a topic in the Google Groups "pymarc Discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pymarc/P8zgjyheABI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pymarc+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pymarc/D574B7A6-2C27-4E77-9616-B6D044B826CB%40pobox.com.
Reply all
Reply to author
Forward
0 new messages