Checking for existence of a tag by number.

2,921 views
Skip to first unread message

Graham Arden

unread,
May 27, 2009, 7:44:09 AM5/27/09
to pydicom
Hi,

If I need to check for the existence of a tag I tend to use:

if "SeriesDescription" in ds:
ds.SeriesDescription = "Removed"

however I need to remove a couple of tags that aren't in the
dictionary. For example GE put a copy of the patient name in [0x009,
0x1040].

I tried:

if [0x009, 0x1040] in ds:
ds.[0x009, 0x1040].value = " "

but it didn't work. Anyone know the solution?

Graham
http://surelythatcantberight.blogspot.com/

Darcy Mason

unread,
May 27, 2009, 2:09:15 PM5/27/09
to pydicom
Hi Graham,
If you use a tuple rather than a list, the check for existence should
work:
if (0x009, 0x1040) in ds:
...
That's because Datasets's __contains__ (called by "in") will try to
make a Tag object from the argument before doing the check -- Tag
accepts tuples (or a single number like 0x0091040) but not lists (I
suppose that little wrinkle could be fixed).

And to remove data elements completely (if you wish) rather than
blanking them, you can use python's del. I recently added some info on
that to the PydicomUserGuide wiki page.
Reply all
Reply to author
Forward
0 new messages