![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Newbie
Join Date: May 2006
Posts: 5
Rep Power: 0
![]() |
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:
http://ar.ch.pwr.wroc.pl/file.php/4/...domowe/opt.log PLEASE HELP! PS Sorry for my english. I'm from Eastern Europe. |
|
|
|
|
|
|
#2 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
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. |
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2006
Posts: 5
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
So... what have you written so far?
|
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#6 |
|
Newbie
Join Date: May 2006
Posts: 5
Rep Power: 0
![]() |
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]] |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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] a = [line for line in list if "Standard orientation:" in line][0] >>> for index, letter in enumerate(["a", "b", "c"]): ... print index, letter ... 0 a 1 b 2 c a = [i for i, x in enumerate(list) if "Standard orientation:" in x][0] 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()) |
|
|
|
|
|
#8 |
|
Newbie
Join Date: May 2006
Posts: 5
Rep Power: 0
![]() |
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 ![]() |
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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
"""
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#10 | |
|
Programming Guru
![]() ![]() |
Quote:
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! |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|