Mattylad
Posts: 98
Joined: Tue Jan 17, 2012 9:59 pm

Reading files into lists etc?

Sun Sep 09, 2012 7:50 pm

Hello, I have been looking at the python tutorials however am still not too clear what I need to do.
I have not done any programming for 20 years, but did learn Pascal & C then.

Generally I struggle to learn something unless I have a purpose in mind so I have found one.
I want to read in a text library file for a CAD package and identify the symbols and components found in it.
Put them in a list/array and be able to output that list.

I then want to be able to read in another form of text library file and make a big list from that.
Then I want to compare the first list against the 3rd list and output the differences only.
Ditto the 2nd list to the 4th list.

So to start off I think I need the basic structure of the program (this does not seem to be identified in the tutorials - it used to be: declare variables, declare functions, main code etc).

Anyway I want to start by pressing a key to ask for the filename to be read in, open a browse window and read in the file.

The file will be have many instances similar to:
.SN54120D :1 ;D_DUAL PULSE SYNCHRONIZERS
SO16 (reflow)
*STM U
*SYM SYNC_120
*EXT 4 2 3 5 1 7 6
*EXT 12 13 14 11 15 9 10
*DFN SN54120D
~thm_power_diss (0.1785)
@Value (SN54120D)
~Manufacturer (TEXAS)
SYNC_120 (ANSI)
4.1!I 2.1!I 3.1!I 5.1!I 1.1!I 7.0!N 6.0!N
SYNC_120 (ANSI)
12.1!I 13.1!I 14.1!I 11.1!I 15.1!I 9.0!N 10.0!N
/GND 8.0!G
/VCC 16.0!P
This is a part.

Each new part/section starts with a full stop.
I.E. .SN54120D
Althought there could be several similar lines such as:
.SN54120D
.SN54120B
.SN54120C
at the top of a part.

And then the line I am looking for is immediately after them.
SO16 (reflow)
this is what I want to read into list 1 as the list of components.

Then I want to look further and distinguish the symbols I.E. the
SYNC_120 (ANSI)
It may be duplicated a few times (as in the above example) or each one may be different.
(so there is going to need to be a compare against existing symbols before adding it to the list.)
This will be list 2.

Then when I have all those read in I want to read in another couple of files, both in the same format to make lists 3 & 4.
I want to look for lines that are similar to:
(SYMDEF L0 "CAPAE1030X1050N-modified" "" (PT 10000000 10000000)
where I want to extract the
"CAPAE1030X1050N-modified" ""
where the component name is within parenthesis.
The 2nd set of parenthesis may also be part of the name (the alternate name) I.E.
"CAPAE1030X1050N-modified" "vertical"
so may have content or may not.
I want to strip the parenthesis off.
If there is a 2nd name (alternate) then I want to enclose that in brackets so it looks like
CAPAE1030X1050N-modified (vertical)
as a single entry in the list.

CAPAE1030X1050N-modified" "vertical"
and
CAPAE1030X1050N-modified" ""
being 2 different components.

So once I have a list 1 of symbols, a list 2 of components, a list 3 of symbols and a list 4 of components.
I then want to be able to output each list as a .csv, then I also want to be able to compare list 1 and list 3 and output the difference into a .csv. The same for list 2 & 4.

I want to start with a simple text based screen program, get the functions right and then progress onto making a fancy gui version.

I want to be able to run this on a windows based PC only.

Now I think I know what I want & need to do, where do you suggest I start?
Is python going to be right for this or should I use VB instead?

Is this the place to get advice on this or is there another forum that would be better?

Thank you.

User avatar
paddyg
Posts: 2496
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Reading files into lists etc?

Sun Sep 09, 2012 8:35 pm

Well I would say
YES, that python is a good way to go because it's quite a good way into 'proper' object based programming but is quite forgiving (aka slack) about defining things up front. It's super flexible for lists (like your several similar lines such as: .SN54120D .SN54120B...) trivial to open and read a text file and has various libraries for parsing stuff.
and NO. this isn't really the place to be asking about python running on a desktop PC as it's all raspberry pi orientated. BUT a main objective of the project is to get people into programming so you will get lots of help for more specific problems. Search on Google and you will get lots of links to people with the same questions (on stackoverflow generally better advice than any of the python documentation pages!!) Good luck with your project.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Mattylad
Posts: 98
Joined: Tue Jan 17, 2012 9:59 pm

Re: Reading files into lists etc?

Tue Sep 25, 2012 7:40 pm

Well I would say that yes this IS the place to ask questions as I am wanting to learn to program, so that eventually I can use my pi's. I do not intend to mess around with them until I have a purpose for them - until then I need a purpose for my learning - and that currently is to be able to read in so,me text file, sort and output differences in them.

It may not be flashing a dalek on the screen but is learning to program just the same as many of the other posts on here that are also not 100% pi based.

So could anyone please give me some pointers, links even to the sort of python coding I need to learn to be able to read these files in, put them in a "what?" array/database/list ?" and so on.

By learning these bits I will be able to start something, without that something I will have no use for learning to code.

Cheers muchly.

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Reading files into lists etc?

Wed Sep 26, 2012 10:32 am

@Mattylad: I think you will need basic Python skills and an understanding of the basic data types (`int`, `str`, `float`) and container types (`str`, `list`, `tuple`, `dict`, `set`). The tutorial in the Python dokumentation gives a tour through all this, but beginners might find tutorials like Learn Python The Hard Way better for starting.

For working with text files one should know the methods on `str`. The `re` module is helpful sometimes — that is an additional language to learn: regular expressions. And for more complex stuff where regular expressions are not powerful engough or get to large and unreadable a parser like PyParsing might be needed.

A program structure for the problem you described could be much the same as it used to be. Except there are (almost) no declarations in Python. Break the problem recursively into smaller sub problems until they are small engough to be solved by some lines of code in a function or method. Choose data structures that can represent the values and fit the usage patterns. Implement own data types (``class``) where it makes sense to bundle data and operations on them. Test the subsolutions and put the small pieces together to solve the original problem.

Just stop telling you want to do this on a PC. Pretend you want to explore if such CAD file processing can be done on a Raspi. ;-)

Code: Select all

while not self.asleep():
    sheep += 1

Mattylad
Posts: 98
Joined: Tue Jan 17, 2012 9:59 pm

Re: Reading files into lists etc?

Wed Sep 26, 2012 6:19 pm

Thank you BJ, I will look into the pointer you have mentioned.

OF course I will be porting this over to the Pi once I have it working :D

User avatar
fdion
Posts: 307
Joined: Sun Sep 16, 2012 2:33 pm
Location: North Carolina, USA
Contact: Website

Re: Reading files into lists etc?

Wed Sep 26, 2012 7:49 pm

Mattylad wrote:Thank you BJ, I will look into the pointer you have mentioned.

OF course I will be porting this over to the Pi once I have it working :D
If you use vim to edit your python code, it is equally fast to edit on a dual quad core (desktop) as the pi itself. http://raspberry-python.blogspot.com/20 ... texto.html (yes, that one page is in spanish, but the commands to get vim going are in english, and there is a google translate button on the right on that page).

You can also get into what I call the "sidekick" mode. You'll feel like you are on your desktop, but you are really developping / running on the Pi.
It's all here (and the follow up article):
http://raspberry-python.blogspot.com/20 ... ekick.html

Francois
Francois
http://raspberry-python.blogspot.com - http://www.3dFutureTech.info - @f_dion

Mattylad
Posts: 98
Joined: Tue Jan 17, 2012 9:59 pm

Re: Reading files into lists etc?

Wed Sep 26, 2012 8:02 pm

Thing is - of course I want to be able to work with the Pi - but first I need to learn and there is little point in rigging up my prize pi just to do that when I have everything I need in my laptop that is sitting in front of me now.

This is exactly the same with hundreds, nay thousands of the other posters here - they are using their computers to learn. So this talk of only discussing it if it involves a pi is IMO wrong.

What matters is that someone (me) is wanting to learn to program so that they can program a Pi once they have learnt.

Return to “Python”