in Education by
This is my code: import SimpleITK as sitk reader = sitk.ImageFileReader() reader.SetFileName(filePath) reader.ReadImageInformation() img = reader.Execute() meta = { "a": reader.GetMetaData('0'), <- if not exist return 'undeinfed' "b": reader.GetMetaData('1'), "c": reader.GetMetaData('2'), } I am a javascript developer. I want to set the meta dict and it shows an error 'Key '0' does not exist'. How to exist in this case? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
The ImageFileReader class has a HasMetaDataKey() boolean function. So you should be able to do something like this: meta = { "a": reader.GetMetaData('0') if reader.HasMetaDataKey('0') else 'undefined', "b": reader.GetMetaData('1') if reader.HasMetaDataKey('1') else 'undefined', "c": reader.GetMetaData('2') if reader.HasMetaDataKey('2') else 'undefined', } And also, you could do in one (long) line: meta = {m: reader.GetMetaData(k) if reader.HasMetaDataKey(k) else 'undefined' for m, k in zip(['a', 'b', 'c'], ['0', '1', '2'])} If you are a beginner then do learn python from scratch.

Related questions

0 votes
    What is the best way to open a file as reading/write if it exists, or if it does not, then create it and ... to do the opening part. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I have the list of sets like so. I basically want to convert this to the dictionary and to address duplicate keys, ... ] = val return d Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I am getting an 'access is denied' error when I attempt to delete a folder that is not empty. I used the ... that is not empty? Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    I have drawn an image in the device context using python and I want to move it smoothly/animate either ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I have drawn an image in the device context using python and I want to move it smoothly/animate either ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I have drawn an image in the device context using python and I want to move it smoothly/animate either ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 15, 2022 in Education by JackTerrance
0 votes
    How can I do the following in Python? array = [0, 10, 20, 40] for (i = array.length() - 1; i >= ... but from the end to the beginning. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I would like to get a list of Python modules, which are in my Python installation (UNIX server). How can ... in your computer? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I want to change a couple of files at one time, iff I can write to all of them. I'm wondering if I ... to this problem look like? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Can anyone tell me how I can represent the equivalent of an Enum in Python? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How can I remove the last character of a string in a newline, In perl I used chomp function what's equivalent to that in Python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to put time delay in the script of Python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    webbrowser.open('https://api.WhatsApp.com/send?phone=number') I want to send WhatsApp messages to numbers without ... in this link. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    This is my code it's just starting the scan but it is not completing ,where is the error in it. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I am trying to create a new column where in, True if last n rows are True in other column. It works fine ... better way to achieve it? Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
...