How do I process this?

Zachary Ware zachary.ware+pylist at gmail.com
Mon Mar 3 23:16:02 EST 2014


On Mon, Mar 3, 2014 at 10:03 PM, Igor Korot <ikorot01 at gmail.com> wrote:
> Hi, ALL,
> I have a csv file which depending on how it was produced gives 2 different
> strings as shown in the example below (test1 and test2).
> I am only interested in the first field in test1 and obviously in the whole
> string of test2.
>
> So, I tried to see if I can get what I want in one simple way, but failed.
> Is it possible to process the file independently?
>
> Thank you.
>
> python
> Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> test1 = "a,,,,"
>>>> test2 = "a"
>>>> (t1,_) = test1.split(',')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ValueError: too many values to unpack
>>>> (t2,_) = test2.split(',')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ValueError: need more than 1 value to unpack
>>>>

I'm not sure what you mean by "is it possible to process the file
independently", can you expand on that a bit?

As far as what you're trying to do, there are a couple ways to do it.
If you can use Python 3 instead:

T:\emp>py -3
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> test1 = "a,,,,"
>>> test2 = "a"
>>> t1, *remainder1 = test1.split(',')
>>> t1
'a'
>>> remainder1
['', '', '', '']
>>> t2, *remainder2 = test2.split(',')
>>> t2
'a'
>>> remainder2
[]
>>> ^Z

Or, if you're stuck on Python 2:

T:\emp>py -2
Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> test1 = "a,,,,"
>>> test2 = "a"
>>> t1 = test1.split(',')[0]
>>> t1
'a'
>>> t2 = test2.split(',')[0]
>>> t2
'a'
>>>

Hope this helps,

-- 
Zach



More information about the Python-list mailing list