Reading data in lists: Error

S

Satish Chimakurthi

Hi all,


This is in continuation of my email today regarding reading data files. I am stuck with one more problem now.

Here are my two data files:

a. fluid_grid.dat

1.00000000000000 0.00000000000000D+000
0.959753969508636 0.280842158538236
0.842255363975169 0.539078752924891
0.656961888321583 0.753923787456623
0.418788196289889 0.908083942512068
0.146905379223583 0.989150549489397

b. fluid_pressure.dat

1.2
2
4
5
3.22

There are six values in each of the two columns in file "fluid_grid.dat" and 6 values in one column in "fluid_pressure.dat".

I read the contents of the first file in two arrays "x" and "y", x contains the values in the first column and y contains the second column values respectively.

I also read the contents of the second file in array "pres"

I wrote the following code for some calculations.

from math import sqrt
ifile4=open('fluid_grid.dat','r')
lines=ifile4.readlines()
x=[]
y=[]
FX=[]
FY=[]
for line in lines:
xi,yi=[float(item) for item in line.replace('D','E').split()]
x.append(xi)
y.append(yi)

pres=[]
pressure_data=open('fluid_pressure.dat','r')


for i in range(1,6):
a=eval(pressure_data.readline())
pres.append(a)

for i in range(1,6):
NXA=y[i-1]-y
NXD=sqrt(((y-y[i-1])**2)+((x-x[i-1]**2))
NYA=x-x[i-1]
NYD=sqrt(((y-y[i-1])**2)+((x-x[i-1]**2))
NX=NXA/NXD
NY=NYA/NYD
FX.append(pres*NX)
FY.append(pres*NY)


I ran this code only to get the following error:

File "<string>", line 24
NYA=x-x[i-1]
^
SyntaxError: invalid syntax


I remember Mr.Skip Montanaro quoting in his email earlier that at the point I make the assignment neither x has no
i'th elements. I am sure this is what is happening now.


Can someone advise any possible work around to this problem ??



Thanks in advance

Best Regards,
Satish Kumar Chimakurthi


SATISH KUMAR CHIMAKURTHI
Graduate Research Assistant
CFD GROUP
Mechanical Engineering
UNIVERSITY OF KENTUCKY
Lexington
KENTUCKY - 40508
U.S.A

Email: (e-mail address removed)
Mobile:859-420-9890
Office: 859-257-6336 X 80691
 
P

Paul McGuire

NXD=sqrt(((y-y[i-1])**2)+((x-x[i-1]**2))
NYA=x-x[i-1] < syntax error raised on this line, because
mismatched ()'s on previous line
NYD=sqrt(((y-y[i-1])**2)+((x-x[i-1]**2))
NX=NXA/NXD

-- Paul
 
P

Peter Otten

I ran this code only to get the following error:
File "<string>", line 24
NYA=x-x[i-1]
^
SyntaxError: invalid syntax


I remember Mr.Skip Montanaro quoting in his email earlier that at the
point I make the assignment neither x has no i'th elements. I am sure this
is what is happening now.


How can you be sure when Python reports a SyntaxError?
Can someone advise any possible work around to this problem ??

See Paul McGuire's post for a fix. As a general rule, you should break messy
expressions into small chunks that are easier to maintain. In your code you
could introduce a distance(x0, y0, x1, y1) function and test it separately
from the rest. Here's another approach that might be helpful if you are
familiar with complex numbers - yes, Python has them built in :)

# assuming pres, x, y as in your post
FZ = []

# you should create the complex numbers directly in the for loop
# reading from the file - I'm just lazy
z = [complex(x0, y0) for x0, y0 in zip(x, y)]

for z0, z1, p in zip(z, z[1:], pres):
d = z1-z0
FZ.append(p*d/abs(d))

# assuming you accidentally swapped FX and FY
FX = [z.real for z in FZ]
FY = [-z.imag for z in FZ] # not sure about the sign

If you're confused by the zip trick in the for loop, here's how it works in
three simple steps:
a = ["a", "b", "c"]
b = ["x", "y", "z"]
zip(a, b)
[('a', 'x'), ('b', 'y'), ('c', 'z')]

zip() will end, when the shortest list is exhausted:
[('a', 'y'), ('b', 'z')]
[('a', 'b'), ('b', 'c')]

I. e, zip(alist, alist[1:]) will create a list of all adjacent pairs in
alist.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top