Re: [Tutor] How parse files in function of number of lines

2014-05-29 Thread Dave Angel
"jarod...@libero.it"  Wrote in message:
> Dear all!
> I have two example files:
> tmp.csv:
> name  value   root
> mark  34  yes
> 
> tmp2.csv
> name  value   root
> 
> 
> I want to print a different text if I have more than one row and if I have 
> only one row. My code is this:
> with open("tmp.csv") as p:
> header =p.next()
> for i in p:
> print i
> g = ()
> if not g:
> print  header
> mark  34  yes
> 
> no
> 
> I want to obtain only where I have only the header the header string? How can 
> I do this?Thnks for your great patience and help!
> 

The most straightforward way is to define a flag that starts
 False, and becomes True if there are any lines after the header.
 

has_body = False
for line in infile:
print line
has_body = True

if has_body:
print header
else:
print "something else"



-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Code Error

2014-05-29 Thread Jude Mudannayake
Dear Sir/Madam

I am currently having some trouble with a python script that I developed and I 
would like to know if you could have a look. I basically am attaching the 
script as part of .py file in this email. The script in its essence is set up 
to take a set of data from a CSV file (containing x,y points) and then arrange 
them into coordinates points i.e. gather the two sets of x and y to form 
coordinates (x1,y1) and (x2, y2) etc... to be plotted as a sketch within ABAQUS 
FE software.. As I run the script I get an error stating the following:

'TypeError: point1; found int, expecting tuple' - This is referring to line 57 
of the code which is essentially the critical command. It is a command that 
instructs to join coordinate points thus creating a line.

The thing is I am pretty sure I have  changed inputs into tuples from line 16 
of the code to 19.

I am not too sure what the issue is here. Could you get back to me with what I 
should look to do in this case.

Thanks in Advance


Jude Mudannayake MSc BEng DIC
Stress Engineer
The National Composites Centre
Feynman Way Central,
Bristol & Bath Science Park,
Emersons Green,
Bristol,
BS16 7FS


Project part financed by the European Union with £9M from the European Regional 
Development Fund 2007-2013, through the South West Regional Development Agency 
under the Competitiveness Operating Programme.

---
This email and any attachments to it may be confidential and are intended 
solely for the use of the individual to whom it is addressed. Any views or 
opinions expressed are solely those of the author and do not necessarily 
represent those of the National Composites Centre (NCC), the National 
Composites Centre Operations Limited (NCCOL) or any of its members or 
associates.
If you are not the intended recipient of this email, you must neither take any 
action based upon its contents, nor copy or show it to anyone.  Please contact 
the sender if you believe you have received this email in error.



csv_coordinatesgen_part.py
Description: csv_coordinatesgen_part.py


trial.csv
Description: trial.csv
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How parse files in function of number of lines

2014-05-29 Thread Wolfgang Maier

On 28.05.2014 21:16, jarod...@libero.it wrote:

Dear all!
I have two example files:
tmp.csv:
namevalue   root
mark34  yes

tmp2.csv
namevalue   root


I want to print a different text if I have more than one row and if I have
only one row. My code is this:
with open("tmp.csv") as p:
 header =p.next()


in general, DON'T use the next method, but the next() function (or 
readline as Alan suggested):

header = next(p)

this will work also in python3, where the next method is gone.

Wolfgang

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Code Error

2014-05-29 Thread Alan Gauld

On 29/05/14 10:48, Jude Mudannayake wrote:


its essence is set up to take a set of data from a CSV file (containing
x,y points) and then arrange them into coordinates points



get an error stating the following:

‘TypeError: point1; found int, expecting tuple’ – This is referring to
line 57 of the code which is essentially the critical command. It is a
command that instructs to join coordinate points thus creating a line.


Don't summarize the traceback. Send the complete error text.
Summarizing usually tells us what you *think* the error is
rather than what Python is actually telling you.


The thing is I am pretty sure I have  changed inputs into tuples from
line 16 of the code to 19.


Maybe...

If the code is not too long (<100 lines) its usually better to embed
it in the message rather than send an attachment...

import csv
f = open ('trial.csv')
csv_f = csv.reader(f)

Ay1 = []
Az1 = []
Ay2 = []
Az2 = []


for row in csv_f:
Ay1.append(eval(row[1]))
Az1.append(eval(row[2]))
Ay2.append(eval(row[3]))
Az2.append(eval(row[4]))


As a general rule don't use eval(). It is very insecure and potentially 
masks errors because if the code is not what you expect strange things 
can silently happen. If you expect integer inputs use int(). Then if its 
not an int, python can tell you. As it stands there is a universe of 
badness that eval can interpret and not tell you a thing.


Ay11 = tuple(Ay1)
Az11 = tuple(Az1)
Ay22 = tuple(Ay2)
Az22 = tuple(Az2)

Here are the lines where you say the error occurs:


for i in range(len(xyCoordsInner)-1):
mySketch.Line(point1=xyCoordsInner[i],
point2=xyCoordsInner[i+1])

for i in range(len(xyCoordsOuter)-1):
mySketch.Line(point1=xyCoordsOuter[i],
point2=xyCoordsOuter[i+1])


Now its not obvious how we get from your lists/tuples
Ay11 etc to xyCoordsInner/Outer? Ok I found it here:

for j in range(0,len(Ay22)):
k2 = (Ay22[j],Az22[j])
#print k2
myList1=k2
xyCoordsOuter = tuple(myList1)
#print xyCoordsOuter

This is a bizarre bit of code.
It loops over a pair of tuples.
It creates a tuple from the tuples.
It sets a second variable to point to the same tuple
It then sets a third variable to point at the same
tuple converted to a tuple(ie the same as it started)
It then goes round the loop again throwing away the
previous tuple until you finally wind up with the
last two elements as a tuple.

You could replace the entire loop with

xyXoordOuter = (Ay22[-1],Az22[-1])

Which is faster and clearer.

Of course if Ay and Az have different lengths you
get into difficulties...

I'm not sure if any of that helps but I'd definitely get
rid of the evals.

And I'd take a close look at the loop above to see
if that's really what you want to do.

And finally I'd print Point1 to see what exactly the
error is talking about.

And if you need more help, send the complete error
trace and embed the code in the message.


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] pylab axis

2014-05-29 Thread Sydney Shall
I would like to start by thanking all the tutors for their wonderful 
genourosity and excellent advice.


My problem concerns forcing pylab to give me the y axis I want.

My code is as follows:

pylab.figure(10)

pylab.title("Ratio of realised capital to advanced capital.")

pylab.xlabel("Time [Cycles of Capital reproduction]")

pylab.ylabel("Ratio of realised capital to advanced capital.")

pylab.xlim = ((0.0, 1.5))

pylab.plot(cycles, ratiocrtoca)

pylab.show(10)


 My pylab output is as follows:


I regret I do not know how to put the pdf file in the message, so I have 
attached the pdf file of the graph. Please guide me on this too.


My problem is that I wish to force the y axis to simply be from 0.0 to 
1.5 without the detail which is just arithmetic noise, I think.


With thanks,

Sydney

--
Sydney Shall



Figure10.pdf
Description: Adobe PDF document
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pylab axis

2014-05-29 Thread Danny Yoo
> My problem concerns forcing pylab to give me the y axis I want.
>
> My code is as follows:
>
> pylab.figure(10)
> pylab.title("Ratio of realised capital to advanced capital.")
> pylab.xlabel("Time [Cycles of Capital reproduction]")
> pylab.ylabel("Ratio of realised capital to advanced capital.")
> pylab.xlim = ((0.0, 1.5))
> pylab.plot(cycles, ratiocrtoca)
> pylab.show(10)
>
>
> My problem is that I wish to force the y axis to simply be from 0.0 to 1.5
> without the detail which is just arithmetic noise, I think.

It appears that you limit the x-axis's bounds by using pylab.xlim().
Have you tried doing the same with pylab.ylim()?


I'm unfamilar with pylab, but have found a tutorial in:
http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut1.html.

I think that 'ylim' is what you're looking for, but I'm not positive yet.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pylab axis

2014-05-29 Thread Mark Lawrence

On 29/05/2014 17:12, Sydney Shall wrote:

I would like to start by thanking all the tutors for their wonderful
genourosity and excellent advice.

My problem concerns forcing pylab to give me the y axis I want.

My code is as follows:

pylab.figure(10)

pylab.title("Ratio of realised capital to advanced capital.")

pylab.xlabel("Time [Cycles of Capital reproduction]")

pylab.ylabel("Ratio of realised capital to advanced capital.")

pylab.xlim = ((0.0, 1.5))

pylab.plot(cycles, ratiocrtoca)

pylab.show(10)


  My pylab output is as follows:


I regret I do not know how to put the pdf file in the message, so I have
attached the pdf file of the graph. Please guide me on this too.

My problem is that I wish to force the y axis to simply be from 0.0 to
1.5 without the detail which is just arithmetic noise, I think.

With thanks,

Sydney

--
Sydney Shall



The joys of the interactive prompt.

In [3]: help(pylab.ylim)
Help on function ylim in module matplotlib.pyplot:

ylim(*args, **kwargs)
Get or set the *y*-limits of the current axes.

::

  ymin, ymax = ylim()   # return the current ylim
  ylim( (ymin, ymax) )  # set the ylim to ymin, ymax
  ylim( ymin, ymax )# set the ylim to ymin, ymax

If you do not specify args, you can pass the *ymin* and *ymax* as
kwargs, e.g.::

  ylim(ymax=3) # adjust the max leaving min unchanged
  ylim(ymin=1) # adjust the min leaving max unchanged

Setting limits turns autoscaling off for the y-axis.

The new axis limits are returned as a length 2 tuple.

Aside, you appear to be using matplotlib in a strange way.  If your code 
is in a script, you'd use a line like this.


import matplotlib.pyplot as plt

If you're working interactively you'd use.

from pylab import *

You have a half way house.

Finally this mailing list is mainly aimed at people learning the Python 
language and not third party modules.  For matplotlib you can find a 
very helpful group here gmane.comp.python.matplotlib.general (and other 
places), I know they don't bite as I've been there :)


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Code Error

2014-05-29 Thread Mark Lawrence

On 29/05/2014 10:48, Jude Mudannayake wrote:

[snipped to pieces]

for i in range(0,len(Ay11)):
k1 = (Ay11[i],Az11[i])
#print k1
myList=k1
xyCoordsInner = tuple(myList)
#print xyCoordsInner

Further to the reply from Alan Gauld, if you're writing code like this 
in Python you're almost certainly doing it wrong.  For loops usually 
look like this.


for ay in Ay11:
doSomeThingWithAy(ay)

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: pylab axis

2014-05-29 Thread Danny Yoo
[Forwarding to tutor@python.org.  Unfortunately, I don't have time to
look at this personally at the moment.  Please use CC to keep the
conversation on the mailing list, so that others can continue to
follow up.]


-- Forwarded message --
From: Sydney Shall 
Date: Thu, May 29, 2014 at 11:40 AM
Subject: Re: [Tutor] pylab axis
To: Danny Yoo 


Dear Danny,
I have tried ylim and it does not present it correctly.
It present the axis on an enlarged with +XXXe-y on the scale, as can
be seen from the pdf of the output. I want to get rid of this extra
bit.
The strange part is that when I first used ylim it did what I wanted,
but now does not.
I am flummoxed.
But thanks anyway.
Sydney


On 29/05/2014 19:09, Danny Yoo wrote:
>>
>> My problem concerns forcing pylab to give me the y axis I want.
>>
>> My code is as follows:
>>
>> pylab.figure(10)
>> pylab.title("Ratio of realised capital to advanced capital.")
>> pylab.xlabel("Time [Cycles of Capital reproduction]")
>> pylab.ylabel("Ratio of realised capital to advanced capital.")
>> pylab.xlim = ((0.0, 1.5))
>> pylab.plot(cycles, ratiocrtoca)
>> pylab.show(10)
>>
>>
>> My problem is that I wish to force the y axis to simply be from 0.0 to 1.5
>> without the detail which is just arithmetic noise, I think.
>
> It appears that you limit the x-axis's bounds by using pylab.xlim().
> Have you tried doing the same with pylab.ylim()?
>
>
> I'm unfamilar with pylab, but have found a tutorial in:
> http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut1.html.
>
> I think that 'ylim' is what you're looking for, but I'm not positive yet.
>

--
Sydney Shall
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] HTML Parsing

2014-05-29 Thread Mitesh H. Budhabhatti
Alan Gauld thanks for the reply.  I'll try that out.

Warm Regards,
*Mitesh H. Budhabhatti*
Cell# +91 99040 83855


On Wed, May 28, 2014 at 11:19 PM, Danny Yoo  wrote:

> > I am using Python 3.3.3 on Windows 7.  I would like to know what is the
> best
> > method to do HTML parsing?  For example, I want to connect to
> www.yahoo.com
> > and get all the tags and their values.
>
>
> For this purpose, you may want to look at the APIs that the search
> engines provide, rather than try to web-scrape the human-focused web
> pages.  Otherwise, your program will probably be fragile to changes in
> the structure of the web site.
>
>
> A search for search APIs comes up with hits like this:
>
> https://developer.yahoo.com/boss/search/
>
> https://developers.google.com/web-search/docs/#fonje_snippets
>
> http://datamarket.azure.com/dataset/bing/search
>
> https://pypi.python.org/pypi/duckduckgo2
>
>
> If you can say more about what you're planning to do, perhaps someone
> has already provided a programmatic interface to it.
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor