Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5026

Re: Inconsistency with split() - Script, OS, or Package Problem?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Inconsistency with split() - Script, OS, or Package Problem?
Date 2011-05-09 16:46 -0400
References <BANLkTi=g2h_BxZDdTdLqyB3nZCukD+Dupw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1357.1304973976.9059.python-list@python.org> (permalink)

Show all headers | View raw


On 5/9/2011 2:10 PM, James Wright wrote:
> Hello,
>
> I have been using a script on several boxes that have been around for
> a while, and everything works just fine.  I am finding though, that on
> some new OS installs the script fails with:
>
> Traceback (most recent call last):
>    File "render4.py", line 114, in<module>
>      create_report_index(each_item)
>    File "render4.py", line 25, in create_report_index
>      [clean_name, _] = each_value.split('_', 1)
> ValueError: need more than 1 value to unpack

What this means is that at some point you tried to split a string that 
does not have an underscore to print on.

 >>> [s,_] = 'no underscore'.split('_',1)
Traceback (most recent call last):
   File "<pyshell#1>", line 1, in <module>
     [s,_] = 'no underscore'.split('_',1)
ValueError: need more than 1 value to unpack

> The OS's are various OpenSuse installs from 10.3 to 11.4.  It works on
> all the existing ones, which leads me to believe that I have a package
> or configuration inconsistency. It should be noted though that I am
> quite new to python programming and very well could have coded in a
> non-portable manner and was just lucky to get it working in the first
> place :)

Your code is fine. The question is,"How could, or why, would new 
installs result in invalid inputs?"

> Here is the code snippet that generates the failure (  when you look
> at this remember that I am a novice :-)  ):
>
> for each_item in D4:  #D4 is a dictionary
>      create_report_index(each_item)

I might write 'for key in D4:' which makes it obvious that D4 is a dict 
and that you are iterating through its keys. Or 'for report_name...'

> def create_report_index(report):  #Here we are creating a simple
> index.html file from data in a text file
>      newfile = open(report + '.html', 'w') #Create the index file using
> report name
>      for each_value in D4[report]:

For debugging, add
            if '_' not in each_value:
               print(report, D4[report], each_value

>          [clean_name, _] = each_value.split('_', 1)
>          newfile.write('<a href="' + report +'//' + each_value +
> '/index.htm">' + clean_name +'</a><BR>\n')

Once you find the offending input, then investigate its source in the 
code that creates D4. Your other questions are premature. If a 
no_underscore each_value is wrong, fix the source. If you decide that 
such are actually ok, then fix the code above:

     if '_' in each_value:
         clean_name = each_value.split('_', 1)
     else:
         clean_name = each_value

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Inconsistency with split() - Script, OS, or Package Problem? Terry Reedy <tjreedy@udel.edu> - 2011-05-09 16:46 -0400

csiph-web