string character count

N

noydb

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"?
 
M

MRAB

noydb said:
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.
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
 
N

nn

noydb said:
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

Also:

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

Emile van Sebille

On 6/30/2009 1:43 PM nn said...
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
>>>
[/QUOTE]

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
 
E

Emile van Sebille

On 6/30/2009 1:43 PM nn said...
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
>>>
[/QUOTE]

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
 
I

Iain King

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"?

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
 
J

Jean-Michel Pichavant

MRAB said:
noydb said:
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.
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).
('/dir1/dir2/test', '.log')
('test', '.log')
('test', '.log')



one safe way would be then9

Jean-Michel
 
N

noydb

thanks everyone for all the ideas -- simple stuff, I know for you all,
but very helpful for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top