Python basic - to remove "LibName:"

I’d like to modify bom_csv_grouped_by_value_with_fp.py but I have never sow any python code before.

The last command in that file is:
out.writerow([refs, len(group), c.getValue(), c.getPartName(), c.getFootprint(),
c.getDescription(), c.getField(“Vendor”)])

c.getFootpint() inserts in output row the tekst “LibName:Footprint”, but I would like to get only “Footprint”.
I suppose I should modify the source to something like this:
footpr=c.getFootprint() - I don’t know if variable have to be defined in any way before
some actions to remove anything up to : from footpr
out.writerow([refs, len(group), c.getValue(), c.getPartName(), footpr,…

How to do it? I believe for some of you it is as simple as 2+2.

c.getFootprint().split(":")[1] should do the trick.

This does not include any error handling! A better option would be to use split to get an array and check its length before extracting the value you want.

fp_split = c.getFootprint().split(":")
if len(fp_split) == 2:
    footprint_name = fp_split[1]
else:
    raise ValueError("Unexpected format for footprint field")

https://docs.python.org/2/library/stdtypes.html#str.split

1 Like

Thanks. Simpler then I expected.
I’ve checked. It works as I wonted.
I believe source data are vell defind so there is no possibility that c.getFootprint() would get something different from LibName:footprint. I don’t expect to use : in libname or footprint name.

Based on your example I have written the PCB name as my csv header:
src_split=net.getSource().split("\")
out.writerow([src_split[len(src_split)-1].split(".")[0]])

Now I have my csv exactly as I need it, so - I’m happy, but…
Then I thought - there should be simpler ways to manipulate strings which are file paths.
Fast look-through python docs you linked I found “Common pathname manipulations”. But don’t know how to understand it.
For example os.path.dirname(path)
I tried net.getSource().dirname(net.getSource()) but got error which I understand as net.getSource() is not path so can not use .dirname with it.
May be there is some way to tell him to treat my text as path.
And I don’t know what I should put as path parameter in (). It looks second time the same info is needed, but it is not logical - it must be something different, but what.

In my source there are \\ but disaperaed when copied here :slight_smile:

In Python instead of src_split[len(src_split)-1] you can do simply src_split[-1] which will always return last element.

os.path.dirname is a function which takes string as argument. You use it like this:
os.path.dirname(net.getSource())

2 Likes

Thanks.
From that I see I need not to use src_split twice so I need not to use that variable at all and that way my two lines:
src_split=net.getSource().split("\\")
out.writerow([src_split[len(src_split)-1].split(".")[0]])
are reduced to single one:
out.writerow([net.getSource().split("\\")[-1].split(".")[0]])

I supposed that may be […] are not needed as I have only one element inside (in all other lines there were more than one), but without [] my name was devided into single chars :slight_smile:

I have tried but got “name ‘os’ is not defined”.
Probably something have to be included in source code before it to tell puthon that I will use os. But I don’t ask for answer. If I will ever find time to learn python I will first read a lot and I think such things will be obvious. My nature is such that I read whole instruction before I use electric cordless kettle.
At that moment I just needed to make a litttle modiffication to bom_csv_grouped_by_value_with_fp.py and I have it done.
After reading all KiCad pdf’s bom generation was the only PCB design step I was afraid if I will be able to do it.

Thanks @Rene_Poschl and @qu1ck for my first two python lessons :slight_smile:

You need to add import os at the top of the script. There are probably some other import lines there already.

Thanks.
I supposed something like that but:

  • I have done what I need,
  • I have reduced this to one line,
  • in “Common pathname manipulations” i didn’t noticed anything like .filename.
    So I will not experiment how to get the same other way and stop with bom and go to build my libraries as I plan to start to seriously use KiCad.

By the way, path name processing code written in this style:
_some_file_path_string.split("\")
will result in non-portable code; it will not work on GNU/Linux, MacOS, etc etc.

By using the path processing functions in the python module “os.path” you can write more portable and readable code.

I didn’t thought about that.
I’m not a programmist and I have never needed my (mainly for own use) code being portable.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.