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

James Wright jamfwright at gmail.com
Mon May 9 15:09:32 EDT 2011


Thank you Steven,

I will take your advice :)  In this particular case though, I do not
think a lack of underscore is the issue, at least as far as I can
understand the issue.  Please see my reply to Ethan.


Thanks,
James


On Mon, May 9, 2011 at 2:43 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Mon, 09 May 2011 14:10:21 -0400, 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
>
> It's a data issue, not an OS or package problem.
>
> Firstly, you don't have to wrap the left hand side in brackets, this
> works fine:
>
>>>> each_value = "aaa_bbb_ccc"
>>>> clean_name, _ = each_value.split('_', 1)
>>>> clean_name
> 'aaa'
>
>
> However, if your data has no underscore at all:
>
>>>> each_value = "aaa*bbb*ccc"
>>>> clean_name, _ = each_value.split('_', 1)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> ValueError: need more than 1 value to unpack
>
>
>
> So you need to allow for the possibility that there is no underscore.
>
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list