f



Reading data in lists: Error

This is a multi-part message in MIME format.

------=_NextPart_000_0064_01C3FD68.AE842DB0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi all,


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

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      =20
 =20
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.=20

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

pres=3D[]
pressure_data=3Dopen('fluid_pressure.dat','r')


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

for i in range(1,6):
        NXA=3Dy[i-1]-y[i]
        NXD=3Dsqrt(((y[i]-y[i-1])**2)+((x[i]-x[i-1]**2))
        NYA=3Dx[i]-x[i-1]
        NYD=3Dsqrt(((y[i]-y[i-1])**2)+((x[i]-x[i-1]**2))
        NX=3DNXA/NXD
        NY=3DNYA/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=3Dx[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=20
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: skchim0@engr.uky.edu
Mobile:859-420-9890
Office: 859-257-6336 X 80691
------=_NextPart_000_0064_01C3FD68.AE842DB0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1400" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi all,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>This is in continuation of my email =
today regarding=20
reading data files. I am stuck with one more problem now. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Here are my two data =
files:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>a. fluid_grid.dat</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;=20
1.00000000000000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
0.00000000000000D+000<BR>&nbsp;&nbsp;=20
0.959753969508636&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
0.280842158538236<BR>&nbsp;&nbsp;=20
0.842255363975169&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
0.539078752924891<BR>&nbsp;&nbsp;=20
0.656961888321583&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
0.753923787456623<BR>&nbsp;&nbsp;=20
0.418788196289889&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
0.908083942512068<BR>&nbsp;&nbsp;=20
0.146905379223583&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
0.989150549489397<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>b. fluid_pressure.dat</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;1.2</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; 2</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;4</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; 5</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;=20
3.22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>There are six values in each of the two =
columns in=20
file "fluid_grid.dat" and 6 values in one column in=20
"fluid_pressure.dat".</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I read the contents of the first file =
in two arrays=20
"x" and "y", x contains the values in the first column and y contains =
the second=20
column values respectively.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I also read the contents of the second =
file in=20
array "pres"</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I wrote the following code for some=20
calculations.&nbsp;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;</DIV></FONT>
<DIV><FONT face=3DArial size=3D2>from math import=20
sqrt<BR>ifile4=3Dopen('fluid_grid.dat','r')<BR>lines=3Difile4.readlines()=
<BR>x=3D[]<BR>y=3D[]<BR>FX=3D[]<BR>FY=3D[]<BR>for=20
line in lines:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
xi,yi=3D[float(item)=20
for item in=20
line.replace('D','E').split()]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;=20
x.append(xi)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
y.append(yi)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial=20
size=3D2>pres=3D[]<BR>pressure_data=3Dopen('fluid_pressure.dat','r')</FON=
T></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV><FONT face=3DArial =
size=3D2>
<DIV><BR>for i in =
range(1,6):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
a=3Deval(pressure_data.readline())<BR>pres.append(a)</DIV>
<DIV>&nbsp;</DIV>
<DIV>for i in range(1,6):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
NXA=3Dy[i-1]-y[i]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
NXD=3Dsqrt(((y[i]-y[i-1])**2)+((x[i]-x[i-1]**2))<BR>&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;=20
NYA=3Dx[i]-x[i-1]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
NYD=3Dsqrt(((y[i]-y[i-1])**2)+((x[i]-x[i-1]**2))<BR>&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;=20
NX=3DNXA/NXD<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
NY=3DNYA/NYD<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
FX.append(pres[i]*NX)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
FY.append(pres[i]*NY)</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>I ran this code only to get the following error:</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;File "&lt;string&gt;", line 24<BR>&nbsp;&nbsp;&nbsp;=20
NYA=3Dx[i]-x[i-1]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ^<BR>SyntaxError: =
invalid=20
syntax<BR></DIV>
<DIV>&nbsp;</DIV>
<DIV>I remember Mr.Skip Montanaro quoting in his email earlier that at =
the point=20
I make the assignment neither x has no <BR>i'th elements. I am sure this =
is what=20
is happening now.</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>Can someone advise any possible work around to this problem =
??</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>Thanks in advance</DIV>
<DIV>&nbsp;</DIV>
<DIV>Best Regards,<BR>Satish Kumar Chimakurthi</DIV>
<DIV>&nbsp;</DIV>
<DIV></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>SATISH KUMAR CHIMAKURTHI<BR>Graduate =
Research=20
Assistant<BR>CFD GROUP<BR>Mechanical Engineering<BR>UNIVERSITY OF=20
KENTUCKY<BR>Lexington<BR>KENTUCKY - 40508<BR>U.S.A</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Email: <A=20
href=3D"mailto:skchim0@engr.uky.edu">skchim0@engr.uky.edu</A><BR>Mobile:8=
59-420-9890<BR>Office:=20
859-257-6336 X 80691</FONT></DIV></BODY></HTML>

------=_NextPart_000_0064_01C3FD68.AE842DB0--


0
skchim0 (24)
2/28/2004 1:34:14 AM
comp.lang.python 77058 articles. 6 followers. Post Follow

2 Replies
575 Views

Similar Articles

[PageSpeed] 34

        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


0
ptmcg1 (29)
2/28/2004 2:01:08 AM
> 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

0
__peter__ (4031)
2/28/2004 10:49:19 AM
Reply: