How do I process this?

Tim Chase python.list at tim.thechases.com
Tue Mar 4 05:09:43 CET 2014


On 2014-03-03 20:03, Igor Korot 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.
> 
> >>> 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
> >>>

Have you tried

  t = test.split(',', 1)[0]

or

  t = test.partition(',')[0]

Both of which yield the same results for me.

-tkc





More information about the Python-list mailing list