Im working on a project where i have to get the number of printers that are allocated to a certain job and need to split the file name up so that i can read the type and the number of printers and if its a half bed or not. The file name will look something similar to this <name>_P<AQT or INI>_B<0-9>[h - for half beds] but would be like 20140702_PAQT_B6h and im wanting the _P and the _B part of it.

This may give you some ideas to get you started ...

# fileTypes.py #

myFiles = [ 'news.txt', 'data.dat', 'programcpp.cpp', 'programc.c' ]

myTypes = [ '.txt', '.dat', '.cpp', '.c' ]


for name in myFiles:
    found = False
    for typ in myTypes:
        if typ in name:
            found = True;
            break;
    if found:
        i = name.find( typ )
        print( i, name[:i], typ )

use split in a loop on "_"

for letter in file.split("_"):
    if letter[2] == 'B':
        do_something
    elif letter[2] == 'P':
        do_something
    else:
        something_else

In the spirit of more than one way to do it, you can also do this:

for i in '20140702_PAQT_B6h'.split('_'):
    if i.startswith('P'):
        print (i)
    elif i.startswith('B'):
        print (i)

Output will be like:

PAQT
B6h

Note however, that you can use this if you are actually reading from a file, you only need to use open then split for line in the file.

In the spirit of more than one way to do it ...

Notice that python came after perl, and in python,

There should be one-- and preferably only one --obvious way to do it.

type

import this

in the python console to read the Zen of python.

The description of the problem is almost a regular expression, so I would do it with the re module.

Hi Gribouillis,

in the python console to read the Zen of python.

I know Zen of Python, but for me

There should be one-- and preferably only one --obvious way to do it.

is just a sugar coated statement, and a sell point advertisment. And am sure you note the word preferably in that statement.

The description of the problem is almost a regular expression, so I would do it with the re module

And there you go, you just mentioned another way of solving the problem. There are several ways to the market in this case.

See the word almost in your own statement. If there is just one way, you would have probably use IS A without the almost

Cheers!

And there you go, you just mentioned another way of solving the problem. There are several ways to the market in this case.

Yes there are severals ways,i was also thinking about regex,but split and startswith works just fine in this simple case.
And may even be better than regex because Readability counts.

>>> s = '20140702_PAQT_B6h'
>>> r = re.search(r'_(P.*)_(B.*)', s)
>>> r.group(1)
'PAQT'
>>> r.group(2)
'B6h'

Perl has There's more than one way to do it (TMTOWTDI)

I think Python's

There should be one-- and preferably only one --obvious way to do it.

Can always be debated,but i think soultion postet here work fine,and to debated about(obvious way) has no point in this case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.