Python – Populate Class from Data in TXT File

After 2 weeks of dirty field work – 22 geotech boreholes in the middle of nowhere, it’s back to GIS. Maybe you’ve been following my recent posts about my little Python project. I have successfully formatted and saved my Python generated data as TXT files that can be read by gINT. Now, I want to be able to read TXT files generated by exporting from gINT. For more background, check out these posts 1), 2) and 3).

A typical file (or portion thereof) would look something like this:

**PROJECT
Title,Title2,Name,Location,Location2,Number,Number2…
GEOTECHNICAL SERVICES,HIGHWAY 999, DALLAS, DALLAS COUNTY, TEXAS, GEO CORP., PROJECT NO.999

**POINT
PointID,HoleDepth,Elevation,Supervisor,Equipment,Date_Start,Date_End,Notes,Note…

B-1,30,,A.G.,CME-75,8/20/2010,8/20/2010,IMORTANT NOTE…
B-2,30,,A.G.,CME-75,8/20/2010,8/20/2010,IMORTANT NOTE…
B-3,30,,A.G.,CME-75,8/20/2010,8/20/2010,IMORTANT NOTE…
B-4,30,,A.G.,CME-75,8/20/2010,8/20/2010,IMORTANT NOTE…

A tag starting with “**” to indicate the class, followed by a line with the attribute names, followed by a line or lines with the attribute data. So how do I read data in a way that allows me to populate the classes I have set up for e.g. PROJECT or POINT data ? This is what I started out using:

 

Of course, this is just another unfinished project. What this gets me is a dictionary with the data to assign to my class instances. After reading a bit about classes and kwargs (keyword arguments) the other day, I am wondering if I need to re-write some of my classes to be more flexible and thus able to use the data from the dictionary directly…

The more I thought about finding the **TAG’s in my TXT I started wondering if it would be easier to work with XML files rather than text. And, in fact, gINT allows me export my data as XML. — I just exported a sample file, and it looks like that would be the smarter way to go because it creates one XML file for every class/object. This way I don’t have to search a TXT file for tags…  and basically the day’s work has been reduced to … well, a mere exercise in programming. 🙂

Advertisements

1 Comment

Filed under Uncategorized

One response to “Python – Populate Class from Data in TXT File

  1. your examples are nice. it is being useful for my project.
    thank u.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s