Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

string character count

0 views
Skip to first unread message

noydb

unread,
Jun 30, 2009, 1:27:57 PM6/30/09
to
If I have a string for a file name such that I want to find the number
of characters to the left of the dot, how can that be done?

I did it this way:
x = "text12345.txt"
dot = x.find('.')
print dot

Was curious to see what method others would use - helps me learn. I
guess I was most curious to see if it could be done in one line.

And, how would a char count be done with no dot -- like if the string
were "textstringwithoutdot" or "no dot in text string"?

MRAB

unread,
Jun 30, 2009, 1:56:39 PM6/30/09
to pytho...@python.org
noydb wrote:
> If I have a string for a file name such that I want to find the number
> of characters to the left of the dot, how can that be done?
>
> I did it this way:
> x = "text12345.txt"
> dot = x.find('.')
> print dot
>
> Was curious to see what method others would use - helps me learn. I
> guess I was most curious to see if it could be done in one line.
>
>>> print "text12345.txt".find('.')
9

> And, how would a char count be done with no dot -- like if the string
> were "textstringwithoutdot" or "no dot in text string"?

If there's no dot then find() returns -1.

If you wanted to know the number of characters before the dot, if
present, or in total otherwise, then you could use split():

>>> len("text12345.txt".split(".", 1)[0])
9
>>> len("textstringwithoutdot".split(".", 1)[0])
20

nn

unread,
Jun 30, 2009, 4:43:03 PM6/30/09
to

Also:

len("text12345.txt".partition('.')[0])
9

Emile van Sebille

unread,
Jun 30, 2009, 6:58:17 PM6/30/09
to pytho...@python.org
On 6/30/2009 1:43 PM nn said...

> On Jun 30, 1:56 pm, MRAB <pyt...@mrabarnett.plus.com> wrote:
<snip>

>> >>> len("text12345.txt".split(".", 1)[0])
>> 9
>> >>> len("textstringwithoutdot".split(".", 1)[0])
>> 20
>
> Also:
>
>

>>> len("text.12345.txt".partition('.')[0])
4
>>> len("text.12345.txt".rpartition('.')[0])
10
>>>

unless you've got file names like text.12345.txt, where you might prefer
rpartition.

>>> len("text.12345.txt".partition('.')[0])
4
>>> len("text.12345.txt".rpartition('.')[0])
10
>>>

Emile

Emile van Sebille

unread,
Jun 30, 2009, 6:58:17 PM6/30/09
to pytho...@python.org
On 6/30/2009 1:43 PM nn said...
> On Jun 30, 1:56 pm, MRAB <pyt...@mrabarnett.plus.com> wrote:
<snip>

>> >>> len("text12345.txt".split(".", 1)[0])
>> 9
>> >>> len("textstringwithoutdot".split(".", 1)[0])
>> 20
>
> Also:
>
>

Iain King

unread,
Jul 1, 2009, 5:52:24 AM7/1/09
to

import os
root, ext = os.path.splitext(x)
print len(root)

or in one line (assuming you've imported os):
print len(os.path.splitext(x)[0])


Iain

noydb

unread,
Jul 1, 2009, 11:45:40 AM7/1/09
to
thanks everyone for all the ideas -- simple stuff, I know for you all,
but very helpful for me.

Jean-Michel Pichavant

unread,
Jul 1, 2009, 6:33:43 AM7/1/09
to MRAB, pytho...@python.org
MRAB wrote:

> noydb wrote:
>> If I have a string for a file name such that I want to find the number
>> of characters to the left of the dot, how can that be done?
>>
>> I did it this way:
>> x = "text12345.txt"
>> dot = x.find('.')
>> print dot
>>
>> Was curious to see what method others would use - helps me learn. I
>> guess I was most curious to see if it could be done in one line.
>>
> >>> print "text12345.txt".find('.')
> 9
>
>> And, how would a char count be done with no dot -- like if the string
>> were "textstringwithoutdot" or "no dot in text string"?
>
> If there's no dot then find() returns -1.
>
> If you wanted to know the number of characters before the dot, if
> present, or in total otherwise, then you could use split():
>
> >>> len("text12345.txt".split(".", 1)[0])
> 9
> >>> len("textstringwithoutdot".split(".", 1)[0])
> 20
>

You may have problems with files containing multiple dots.
the os module provide nice & safe methods to parse a file path (on any
system, mac os windows, linux).

>>>f = "/dir1/dir2/test.log"
>>>os.path.splitext(f)
('/dir1/dir2/test', '.log')

>>>os.path.splitext(os.path.basename(f))
('test', '.log')

>>>f2 = 'test.log'
>>>os.path.splitext(os.path.basename(f2))
('test', '.log')

one safe way would be then
>>> import os
>>> x = "text12345.txt"
>>> base , ext = os.path.splitext(os.path.basename(x))
>>> print len(base)
9

Jean-Michel

0 new messages