0

I know this is horrible coding. Just trying to find out why do i keep getting this error message...

QUESTION: When not supplying the file with the script; why does it return the error below instead of the except block? else the script works if I supply the xml file when running it.

Traceback (most recent call last): File "C:\bin\read.py", line 5, in script, filename = argv ValueError: need more than 1 value to unpack

import sys
from xml.dom import minidom
from sys import argv

script, filename = argv

xmldoc = minidom.parse(filename)
total = len(sys.argv)
spacenumber = 1
space = "\t" * spacenumber

betweenspace = "\t" * 2

# ROOT ELEMENT <clusterConfig></clusterConfig>
rootElement = xmldoc.getElementsByTagName('clusterConfig')[0]

# CHILD ELEMENT 1 <instanceConfiguration></instanceConfiguration>
#childElement1s = rootElement.getElementsByTagName('instanceConfiguration')[0]
instanceRaters      = rootElement.getElementsByTagName('instanceConfiguration')[0]
instanceUpdaters    = rootElement.getElementsByTagName('instanceConfiguration')[1]
instanceGuiders     = rootElement.getElementsByTagName('instanceConfiguration')[2]
instanceBulkLoaders = rootElement.getElementsByTagName('instanceConfiguration')[3]
instanceTaxers      = rootElement.getElementsByTagName('instanceConfiguration')[4]
instanceDispatchers = rootElement.getElementsByTagName('instanceConfiguration')[5]

# CHILD ELEMENT 2 <configParameter />
#childElement2s = childElement1s.getElementsByTagName('configParameter')
raterAttributes      = instanceRaters.getElementsByTagName('configParameter')
updaterAttributes    = instanceUpdaters.getElementsByTagName('configParameter')
guiderAttributes     = instanceGuiders.getElementsByTagName('configParameter')
bulkLoaderAttributes = instanceBulkLoaders.getElementsByTagName('configParameter')
taxerAttributes      = instanceTaxers.getElementsByTagName('configParameter')
dispatcherAttributes = instanceDispatchers.getElementsByTagName('configParameter')

def runthis():
    # Print rater configuration
    print "RATER CONFIGURATION:\n"
    for raterAttribute in raterAttributes:
        print ("%s%s%s%s" %(space,raterAttribute.getAttribute('name'), betweenspace, raterAttribute.getAttribute('value')))
    # Print updater configuration
    print "\n\nUPDATER CONFIGURATION:\n"
    for updaterAttribute in updaterAttributes:
        print ("%s%s%s%s" %(space,updaterAttribute.getAttribute('name'), betweenspace, updaterAttribute.getAttribute('value')))
    # Print guider configuration
    print "\n\nGUIDER CONFIGURATION:\n"
    for guiderAttribute in guiderAttributes:
        print ("%s%s%s%s" %(space,guiderAttribute.getAttribute('name'), betweenspace, guiderAttribute.getAttribute('value')))
    # Print bulkLoader configuration
    print "\n\nBULKLOADER CONFIGURATION:\n"
    for bulkLoaderAttribute in bulkLoaderAttributes:
        print ("%s%s%s%s" %(space,bulkLoaderAttribute.getAttribute('name'), betweenspace, bulkLoaderAttribute.getAttribute('value')))
    # Print taxer configuration
    print "\n\nTAXER CONFIGURATION:\n"
    for taxerAttribute in taxerAttributes:
        print ("%s%s%s%s" %(space,taxerAttribute.getAttribute('name'), betweenspace, taxerAttribute.getAttribute('value')))
    # Print dispatcher configuration
    print "\n\nDISPATCHER CONFIGURATION:\n"
    for dispatcherAttribute in dispatcherAttributes:
        print ("%s%s%s%s" %(space,dispatcherAttribute.getAttribute('name'), betweenspace, dispatcherAttribute.getAttribute('value')))

try:
    runthis()
except ValueError:
    print "ERROR!!!! YOU FORGOT TO INCLUDE THE PARAMETERS.XML FILE!"
Khorem
  • 61
  • 5
  • 1
    try this instead `script, filename = argv[0], argv[1]` – kyle k Dec 18 '14 at 01:23
  • 2
    How are you calling the script? Do you pass a filename to it? Try `print argv`. – Peter Wood Dec 18 '14 at 01:23
  • 1
    repeated question: [https://stackoverflow.com/questions/2814128/python-error-valueerror-need-more-than-1-value-to-unpack?rq=1][1] [1]: https://stackoverflow.com/questions/2814128/python-error-valueerror-need-more-than-1-value-to-unpack?rq=1 – adrianX Dec 18 '14 at 01:33
  • If the error is on line 5, lines 6 and following are a bit superfluous, really. – tdelaney Dec 18 '14 at 02:00

1 Answers1

0

From my understanding, it seems that the ValueError: need more than 1 value to unpack arises when you fail to pass sufficient arguments to argv, the argument variable.

For example, if you are calling the script through, say a command line terminal, you would need to key python script.py filename. (Where script.py is the script name and filename is the file you wish to assign to the variable filename.)

If you simply type python script.py, you will return the ValueError that you described. This is because by using argv, you are required to input the variable at the point when you call the script.

Hope this helps, please do ask if unclear/if this is does not solve the issues.

Deem
  • 7,007
  • 2
  • 19
  • 23
  • Sorry i failed to mention that part. The script works if i pass the value. If I don't pass the value how do i catch the error and display Usage? I've tried several method already available on the search but couldn't find the solution. I keep getting the same error message. – Khorem Dec 18 '14 at 15:09
  • I'm sorry, but I'm not exactly sure what you're asking. The script works if you pass the value, because you have to pass the value. You cannot unpack a variable from argv that has not been passed to it. Perhaps you could try looking at [Python Error: “ValueError: need more than 1 value to unpack”](http://stackoverflow.com/questions/2814128/python-error-valueerror-need-more-than-1-value-to-unpack) to see if it answers your queries. – Deem Dec 22 '14 at 05:13
  • How would i handle the part where user doesn't pass the value? – Khorem Dec 23 '14 at 14:23
  • If the user does not pass a value, the script cannot continue to run. That's how `argv` works. If you say that `argv`will be unpacked into 'script' and 'filename', then those two values must be passed to it, or else it will not be able to continue. Perhaps you would like to try using `raw_input()` (Python 2.x) or `input()` (Python 3.x) for the user to pass the value instead. – Deem Dec 25 '14 at 08:40