By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,319 Members | 2,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,319 IT Pros & Developers. It's quick & easy.

Reading data in lists: Error

P: n/a
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[i]
NXD=sqrt(((y[i]-y[i-1])**2)+((x[i]-x[i-1]**2))
NYA=x[i]-x[i-1]
NYD=sqrt(((y[i]-y[i-1])**2)+((x[i]-x[i-1]**2))
NX=NXA/NXD
NY=NYA/NYD
FX.append(pres[i]*NX)
FY.append(pres[i]*NY)
I ran this code only to get the following error:

File "<string>", line 24
NYA=x[i]-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: sk*****@engr.uky.edu
Mobile:859-420-9890
Office: 859-257-6336 X 80691
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


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

-- Paul
Jul 18 '05 #2

P: n/a
> I ran this code only to get the following error:

File "<string>", line 24
NYA=x[i]-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:
b[1:] ['y', 'z'] zip(a, b[1:]) [('a', 'y'), ('b', 'z')]
zip(a, a[1:])

[('a', 'b'), ('b', 'c')]

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

Peter

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.