Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 29th, 2006, 11:53 AM   #1
arranta
Newbie
 
Join Date: May 2006
Posts: 5
Rep Power: 0 arranta is on a distinguished road
Please help me graduate!

I'm girl and I'm not into computers and programming. I'm studying chemistry and I have a course of programming in python. I don't know much about it but this is the last thing I need to pass to get my M.Sc. in chemistry.

I need to write a simple (I guess) programm and send it to my teacher until midnight tommorow. Please help me!

This is my taski:
Quote:
Write a program which reads a file and prints atoms' coordinates. Thease coordinates are in the line beginning with "Standard orientation:". Be careful. This line occures few times in the input file so you need to use the last one.

                         Standard orientation:                         

 ---------------------------------------------------------------------

 Center     Atomic     Atomic              Coordinates (Angstroms)

 Number     Number      Type              X           Y           Z

 ---------------------------------------------------------------------

    1          8             0        1.134793   -0.271635   -0.000036

    2          6             0        0.144813    0.402688    0.000046

    3          8             0       -1.115077   -0.083365   -0.000108

    4          1             0        0.115449    1.476364    0.000260

    5          1             0       -1.142054   -1.052488    0.000615

 ---------------------------------------------------------------------

A name of the file should be an argument used in shell and should print results in four columns - atomic number, x coordinate, y coordinate, z coordinate).
A file to use in this program you can find here:
http://ar.ch.pwr.wroc.pl/file.php/4/...domowe/opt.log

PLEASE HELP!

PS Sorry for my english. I'm from Eastern Europe.
arranta is offline   Reply With Quote
Old May 29th, 2006, 12:04 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by arranta
I need to write a simple (I guess) programm and send it to my teacher until midnight tommorow. Please help me!
What have you created so far?

I can't think of much advice I can give without completing the task for you. You should probably use the re.split command to turn a single line of numerical data into a list of numbers. You should also read each line in the file into a list for easy processing.

Apart from this, it's difficult to give any advice without seeing the program you have constructed, or explaining what part of the assignment you are having problems with.
Arevos is offline   Reply With Quote
Old May 29th, 2006, 12:36 PM   #3
arranta
Newbie
 
Join Date: May 2006
Posts: 5
Rep Power: 0 arranta is on a distinguished road
I'm doing exactly the same thing. I'm converting data into list of lines. It's very hard for me. I'm reading the manuals and trying to create somethin but it is a pain in the a..

thanx
arranta is offline   Reply With Quote
Old May 29th, 2006, 12:52 PM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
So... what have you written so far?
Arevos is offline   Reply With Quote
Old May 29th, 2006, 1:04 PM   #5
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
Sorry, I can't help, as I don't know Python, but I just want to say that your English is almost perfect. The only thing is that we would say "by midnight tommorow" rather than "until midnight tomorrow".
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials.
--WilliamSChips on Slashdot
uman is offline   Reply With Quote
Old May 29th, 2006, 1:08 PM   #6
arranta
Newbie
 
Join Date: May 2006
Posts: 5
Rep Power: 0 arranta is on a distinguished road
I realize that it could be total crap but...

file=open('opt.log')
   list=file.readlines()
   list.reverse()
   A=list.index (Standard orientation:)
   list_new=[['A'+5],['A'+9]]
arranta is offline   Reply With Quote
Old May 29th, 2006, 3:42 PM   #7
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
That's a good start. The first problem you have in your code is that list.index will only find exact matches. This is a problem because there appears to be a lot of whitespace in the file.

Instead, you probably want a command which says, "Find me the first line that has the 'Standard orientation' text in it". List comprehensions come in useful here: you can filter a list based on certain criteria.
stdori_list = [line for line in list if "Standard orientation:" in line]
And you can use standard indices to get the first value:
a = [line for line in list if "Standard orientation:" in line][0]
However, this isn't much good, as it returns the line itself, rather than the index. The enumerate function was introduced in Python 2.4, which hopefully you'll have. This function adds in the index to any for loop or list comprehension. For instance:
>>> for index, letter in enumerate(["a", "b", "c"]):
...    print index, letter
...    
0 a
1 b
2 c
Using this, you can get the index thus:
a = [i for i, x in enumerate(list) if "Standard orientation:" in x][0]
From here, you'll need to find the lines that contain numerical data. I'll leave that as an exercise for the reader.

Once you have the lines, there is a simple way to extract the data, using the re.split function. Again, I won't go into much detail, but you can split up a line based on a regular expression.

Regular expressions are a kind of universal sub-language for text searching and manipulation. It contains several special symbols. The two which you need are \s, which means "any whitespace", and +, which means "one or more of the previous character. Thus, the regular expression "\s+" means "one or more whitespace".

Using this in the split command, getting information from a line can be as easy as:
center, number, type, x, y, z = re.split("\s+", line.strip())
(The .strip() method removes trailing and preceeding whitespace)
Arevos is offline   Reply With Quote
Old May 29th, 2006, 4:36 PM   #8
arranta
Newbie
 
Join Date: May 2006
Posts: 5
Rep Power: 0 arranta is on a distinguished road
Thank you very much. I'll check it out in a moment. There is one concern I've got. What is enumerate function and how to check out if I have it?

Sorry for super-noobie-like questions but as I said. I'm dealing more with chemicals rather than computers
arranta is offline   Reply With Quote
Old May 29th, 2006, 4:46 PM   #9
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

I can't get a hold of your data file, but you got that part figured out anyway. So here is my wooden nickle's worth:
# let's assume that opt.log looks like str1

str1 = """\
                        Standard orientation:

 ---------------------------------------------------------------------

 Center     Atomic     Atomic              Coordinates (Angstroms)

 Number     Number      Type              X           Y           Z

 ---------------------------------------------------------------------

    1          8             0        1.134793   -0.271635   -0.000036

    2          6             0        0.144813    0.402688    0.000046

    3          8             0       -1.115077   -0.083365   -0.000108

    4          1             0        0.115449    1.476364    0.000260

    5          1             0       -1.142054   -1.052488    0.000615

 ---------------------------------------------------------------------
"""

list1 = str1.split("\n")
numeric_list = []
for line in list1:
    # strip leading whitspace
    line = line.lstrip()
    # find all the lines that start with a number
    # and extract all the numbers, no spaces
    if len(line) > 0 and line[0].isdigit():
        numeric_list.append(line.split(None))

print "%14s %14s % 14s %14s" % ("atomic number", "x coordinate", "y coordinate", "z coordinate")
for line in numeric_list:
    print "%14s %14s % 14s %14s" % (line[1], line[3], line[4], line[5])

"""
 atomic number   x coordinate   y coordinate   z coordinate
             8       1.134793      -0.271635      -0.000036
             6       0.144813       0.402688       0.000046
             8      -1.115077      -0.083365      -0.000108
             1       0.115449       1.476364       0.000260
             1      -1.142054      -1.052488       0.000615
"""
If you want to see what's going on, then insert some interim print statements like print list1, or print line, or print numeric_list. I think its easy to understand this way.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old May 29th, 2006, 5:09 PM   #10
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
Quote:
Originally Posted by arranta
Thank you very much. I'll check it out in a moment. There is one concern I've got. What is enumerate function and how to check out if I have it?

Sorry for super-noobie-like questions but as I said. I'm dealing more with chemicals rather than computers
Don't say sorry for asking questions! That's what this forum is for. Now, had you asked us to code it for you, it might have been a different story. Our users have no problem answering newbie questions as long as the user has shown some effort on their part (like you have).

Keep asking questions, it's the only real way to learn :-)
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC