Hello Gabriel cant help noticing you
Hi Gabriel I couldnt help but to write this mail. I am not a python programmer just a shell script coder and i am planning to learn some decent python and raise my level from a shell script junkie to a Python gentleman the way you explain things and show it in code is simply amazing. I particularly liked the way you explained that Regular expression stuff to parse that XML i am just awestruck. NO BOOK/ARTICLE EVER OUT THERE IN MY MIND HAS DONE IT SO CLEANLY AS YOU DID IT. regards Hrishy ___ Yahoo! Answers - Got a question? Someone out there knows the answer. Try it now. http://uk.answers.yahoo.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Hello Gabriel cant help noticing you
Hi Gabriel Well it could be a bad example when we need to do lot more things via xml but..the regex in that i have preserved it carefully thats key takeawy for a beginner like me the way the problem was dissected to arrive at what was needed regards Hrishy --- Gabriel Genellina <[EMAIL PROTECTED]> wrote: > En Wed, 26 Sep 2007 03:35:25 -0300, hrishy > <[EMAIL PROTECTED]> escribi�: > > > I couldnt help but to write this mail. > > > > I am not a python programmer just a shell script > coder > > and i am planning to learn some decent python and > > raise my level from a shell script junkie to a > Python > > gentleman the way you explain things and show it > in > > code is simply amazing. > > Thanks! I feel somewhat embarrassed now :"> > Glad to see you find my posts useful. > > > I particularly liked the way you explained that > > Regular expression stuff to parse that XML i am > just > > awestruck. > > Thanks again! But parsing XML is not a good example > of RE usage, so I > don't like it so much. > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list ___ Want ideas for reducing your carbon footprint? Visit Yahoo! For Good http://uk.promotions.yahoo.com/forgood/environment.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Equivalent of 'wget' for python?
Hi Please excuse my OOP but is my understanding correct urllib.urlretrieve(url_of_zip_file,destination_on_local_filesystem) is urllib --->Static Class on which the method urlretrieve method is invoked ? In that case what does the python 3.0 version mean import urllib.request urllib.request.urlretrieve(url, local_file_name) urllib -->static class request -->method urlretrieve--> what is this then ? regards Hrishy --- On Mon, 8/12/08, Jerry Hill <[EMAIL PROTECTED]> wrote: > From: Jerry Hill <[EMAIL PROTECTED]> > Subject: Re: Equivalent of 'wget' for python? > To: [email protected] > Date: Monday, 8 December, 2008, 5:54 PM > On Mon, Dec 8, 2008 at 11:53 AM, r0g > <[EMAIL PROTECTED]> wrote: > > urllib.urlretrieve(url_of_zip_file, > destination_on_local_filesystem). > > In python 3.0, that appears to be: > > import urllib.request > urllib.request.urlretrieve(url, local_file_name) > > -- > Jerry > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Equivalent of 'wget' for python?
Hi Saju Thanks for helping the oop challenged regards Hrishy --- On Thu, 11/12/08, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > From: [EMAIL PROTECTED] <[EMAIL PROTECTED]> > Subject: Re: Equivalent of 'wget' for python? > To: [email protected] > Date: Thursday, 11 December, 2008, 10:41 AM > On Dec 11, 3:36 pm, "[EMAIL PROTECTED]" > <[EMAIL PROTECTED]> > wrote: > > On Dec 11, 2:36 pm, hrishy > <[EMAIL PROTECTED]> wrote: > > > > > Hi > > > > > Please excuse my OOP but is my understanding > correct > > > > > > urllib.urlretrieve(url_of_zip_file,destination_on_local_filesystem) > > > > > is urllib --->Static Class on which the method > urlretrieve method is invoked ? > > > > No urllib is a "method". Use type(obj) to > find out what python thinks > > typo c/method/module > > > the "type" of that object is. Note that > "object" here is not meant in > > the same sense as the OOP definition. > > > > > > > > > In that case what does the python 3.0 version > mean > > > > > import urllib.request > > > urllib.request.urlretrieve(url, local_file_name) > > > > > urllib -->static class > > > request -->method > > > urlretrieve--> what is this then ? > > > > A 'function'. urllib.request.urlretrieve is > the fully qualified name > > of the function urlretrieve. In other words > urlretrieve lives in the > > urllib.request namespace. > > > > -srp > > > > > > > > > regards > > > Hrishy > > > > > --- On Mon, 8/12/08, Jerry Hill > <[EMAIL PROTECTED]> wrote: > > > > > > From: Jerry Hill > <[EMAIL PROTECTED]> > > > > Subject: Re: Equivalent of 'wget' > for python? > > > > To: [EMAIL PROTECTED] > > > > Date: Monday, 8 December, 2008, 5:54 PM > > > > On Mon, Dec 8, 2008 at 11:53 AM, r0g > > > > <[EMAIL PROTECTED]> wrote: > > > > > urllib.urlretrieve(url_of_zip_file, > > > > destination_on_local_filesystem). > > > > > > In python 3.0, that appears to be: > > > > > > import urllib.request > > > > urllib.request.urlretrieve(url, > local_file_name) > > > > > > -- > > > > Jerry > > > > -- > > > > >http://mail.python.org/mailman/listinfo/python-list > > > > > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: XML Parsing
Hi I am just a python enthusiast and not a python user but was just wundering why didnt the list members come up with or recommen XPATH based solution which i think is very elegant for this type of a problem isnt it ? regards Hrishy --- On Wed, 25/2/09, Lie Ryan wrote: > From: Lie Ryan > Subject: Re: XML Parsing > To: [email protected] > Date: Wednesday, 25 February, 2009, 5:43 AM > On Tue, 24 Feb 2009 20:50:20 -0800, Girish wrote: > > > Hello, > > > > I have a xml file which is as follows: > > > > > > > > Id="pid_031605_093137_283"> > > > $ > > PID > > > > > > .. > > ... > > > > Can anyone please tell me how to get content of > tag.. that is, > > how to extract the data "![CDATA[Parameter > Identifiers Supported - $01 > > to $20]]" > > > > Thanks, > > Girish... > > The easy one is to use re module (Regular expression). > > # untested > import re > signal_pattern = > re.compile('(.*)') > signals = signal_pattern.findall(xmlstring) > > also, you may also use the xml module, which will be more > reliable if you > have data like this: attr="blooo">blah, > > >>> import xml.dom.minidom > >>> xmldata = > xml.dom.minidom.parse(open('myfile.xml')) > >>> for node in > xmldata.getElementsByTagName('Signal'): print > node.toxml() > ... > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: XML Parsing
Hi Something like this http://mail.python.org/mailman/listinfo/python-list
Re: XML Parsing
Hi Lie I am not a python guy but very interested in the langauge and i consider the people on this list to be intelligent and was wundering why you people did not suggest xpath for this kind of a problem just curious and willing to learn. I am searching for a answer but the question is why not use xpath to extract xml text from a xml doc ? regards Hrishy --- On Wed, 25/2/09, Lie Ryan wrote: > From: Lie Ryan > Subject: Re: XML Parsing > To: [email protected] > Date: Wednesday, 25 February, 2009, 7:33 AM > Are you searching for answer or searching for another people > that have > the same answer as you? :) > > "Many roads lead to Rome" is a very famous > quotation... > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: XML Parsing
Ha the guru himself responding :-) --- On Wed, 25/2/09, Paul McGuire wrote: > From: Paul McGuire > Subject: Re: XML Parsing > To: [email protected] > Date: Wednesday, 25 February, 2009, 2:04 PM > On Feb 25, 1:17 am, hrishy > wrote: > > Hi > > > > Something like this > > > > > > > Note i am not a python programmer just a enthusiast > and i was curious why people on the list didnt suggest a > code like above > > > > You just beat the rest of us to it - good example of > ElementTree for > parsing XML (and I Iearned the '//' shortcut for > one or more > intervening tag levels). > > To the OP: if you are parsing XML, I would look hard at the > modules > (esp. ElementTree) that are written explicitly for XML, > before > considering using regular expressions. There are just too > many > potential surprises when trying to match XML tags - > presence/absence/ > order of attributes, namespaces, whitespace inside tags, to > name a > few. > > -- Paul > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: XML Parsing
Hi Cliff Thanks so using elementree is the right way to handle this problem regards Hrishy --- On Wed, 25/2/09, J. Clifford Dyer wrote: > From: J. Clifford Dyer > Subject: Re: XML Parsing > To: [email protected] > Cc: [email protected], "Lie Ryan" > Date: Wednesday, 25 February, 2009, 12:37 PM > Probably because you responded an hour after the question > was posted, > and in the dead of night. Newsgroups often move slower > than that. But > now we have posted a solution like that, so all's well > in the world. :) > > Cheers, > Cliff > > > On Wed, 2009-02-25 at 08:20 +, hrishy wrote: > > Hi Lie > > > > I am not a python guy but very interested in the > langauge and i consider the people on this list to be > intelligent and was wundering why you people did not suggest > xpath for this kind of a problem just curious and willing to > learn. > > > > I am searching for a answer but the question is > > why not use xpath to extract xml text from a xml doc ? > > > > regards > > Hrishy > > > > > > --- On Wed, 25/2/09, Lie Ryan > wrote: > > > > > From: Lie Ryan > > > Subject: Re: XML Parsing > > > To: [email protected] > > > Date: Wednesday, 25 February, 2009, 7:33 AM > > > Are you searching for answer or searching for > another people > > > that have > > > the same answer as you? :) > > > > > > "Many roads lead to Rome" is a very > famous > > > quotation... > > > > > > -- > > > > http://mail.python.org/mailman/listinfo/python-list > > > > > > > > -- > > http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list
Linq to Python
Hi Will LINQ be ported to Python ? regards Hrishy -- http://mail.python.org/mailman/listinfo/python-list
Re: Linq to Python
Hi Thanks for those links however LINQ seems to be much more then ORM tool it can for example join an XML file with a relational datasource or create a XSD regards Hrishy --- On Tue, 23/9/08, Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > From: Diez B. Roggisch <[EMAIL PROTECTED]> > Subject: Re: Linq to Python > To: [email protected] > Date: Tuesday, 23 September, 2008, 4:06 PM > hrishy wrote: > > > Hi > > > > Will LINQ be ported to Python ? > > Take a look at SQLAlchemy or SQLObject for python-based > ORM/SQL-abstractions. > > Apart from that, python is already heavily based on > concepts like iterators, > filtering. Take a look at itertools. > > Diez > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Linq to Python
Hi Terry Oops i never realised the mistake i have commited I apologise (i thought changing the subject line would make a new thread) I apologise (I thought Python programmers were smart and they did know what LINQ was) I don't apologise ( i dont apologise for the third one not sounding cocky here but i did google but nothing much came up thats when i posted the question here since i always see Python programmers are somehow smarter then programmers in other langauges i don't know if its the language or the programmers themselves that make them smart) regards Hrishy --- On Tue, 23/9/08, Terry Reedy <[EMAIL PROTECTED]> wrote: > From: Terry Reedy <[EMAIL PROTECTED]> > Subject: Re: Linq to Python > To: [email protected] > Date: Tuesday, 23 September, 2008, 7:51 PM > > Will LINQ be ported to Python ? > > I have three suggestions: > > 1. When starting a new thread, start a *new* thread. > Don't tack a new, > unrelated subject onto an existing thread. Your post will > not be seen > by people with readers that collapse thread and who do not > happen to > read the 'Python is slow?' thread. > > 2. Also, give enough informaton that people can understand > your question > without searching the web and guessing. In particular, > that do *you* > mean by LINQ? The game? The .NET component? Or something > else? > > 3. Before posting, search the Python manuals or the web a > bit for an > answer. If you mean the .NET component, googling > 'Python LINQ' should > partly answer your question. > > tjr > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Linq to Python
Hi Well wouldn't it be a lot easier to query and join a xml source with a relational source with LINQ capabilites in Python. Hmm what am i missing here is there a site that takes all LINQ examples and does them using list comprehensions and makes them sound easy ? wasn't python supposed to make everything easy ? regards Hrishy --- On Tue, 23/9/08, sturlamolden <[EMAIL PROTECTED]> wrote: > From: sturlamolden <[EMAIL PROTECTED]> > Subject: Re: Linq to Python > To: [email protected] > Date: Tuesday, 23 September, 2008, 7:49 PM > On Sep 23, 4:48 pm, hrishy <[EMAIL PROTECTED]> > wrote: > > > Will LINQ be ported to Python ? > > No, because Python already has list comprehensions and we > don't need > the XML buzzword. > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Linq to Python
Hi Tom This is what i like and feel of the Python programmers smarter then every other langauge i know of. But i am not comfortable with your second statement XML i never need it one day everybody would need it. regards Hrishy --- On Tue, 23/9/08, Thomas G. Willis <[EMAIL PROTECTED]> wrote: > From: Thomas G. Willis <[EMAIL PROTECTED]> > Subject: Re: Linq to Python > To: [email protected] > Date: Tuesday, 23 September, 2008, 7:45 PM > > But surely the idea behind it will eventually spread. > It's really > > just comprehensions generalized over XML and > relational datasets, a > > noble goal. Besides, it's main purpose for .NET > was to bring > > functional programming to it. Python already has > that, somewhat... > > it's really any object out of the box, i think the sql > linq stuff is > more of a query compiler, IMO sqlalchemy does that. > > > query = select(user_cols, > > and_(table_relationship.c.accept_user_id==user.id, > > table_relationship.c.start_date==None > > ), > > from_obj=join( > > table_relationship,table_user, > > > onclause=table_user.c.id==table_relationship.c.init_user_id > > ).outerjoin(table_profile) > > ) > > session.execute(query).fetchall() > > > > > > > XML? meh hopefully I would never need it. :) > > > C# is my day job, and when I got my hands on LINQ back in > January my > initial thought was "Finally I have list > comprehensions day job is > fun again" > > For the most part, I think C# is catching up. > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Linq to Python
Hi
If i rephrase my question how will i do this in Python
http://informationr.net/ir/13-2/TB0806.html
Watch this query on the page Where he joins all different kind of things with
ease and elegance(as per my opinion)
[code]
var stoogeGuys =
Beginning with the XML source
from xmlGuys in xmlSource.Descendants("Stooge")
Join to the array on the common element "stoogeName"
join arrayGuys in familyFacts
on xmlGuys.Element("stoogeName").Value equals arrayGuys.stoogeName
Join to the database on the common element "stoogeName"
join dbGuys in stoogeContext.stoogeTables
on xmlGuys.Element("stoogeName").Value equals dbGuys.stoogeName
select new
{
firstName= dbGuys.stoogeName,
familyName = arrayGuys.familyName,
birthDate= xmlGuys.Element("birthDate").Value,
deathDate= xmlGuys.Element("deathDate").Value,
hairCutStyle = dbGuys.stoogeHaircut,
};
[/code]
regards
Hrishy
--
http://mail.python.org/mailman/listinfo/python-list
Re: Linq to Python
Hi Grant haha :-) i discounted that perspective :-) regards Hrishy --- On Thu, 25/9/08, Grant Edwards <[EMAIL PROTECTED]> wrote: > From: Grant Edwards <[EMAIL PROTECTED]> > Subject: Re: Linq to Python > To: [email protected] > Date: Thursday, 25 September, 2008, 2:22 AM > On 2008-09-24, Bruno Desthuilliers > <[EMAIL PROTECTED]> wrote: > > hrishy a écrit : > > (snip) > > > > > >> I apologise > >> (I thought Python programmers were smart and they > did know what LINQ was) > > > > Is there really any relation between "being > smart" and knowing anything > > about the latest MS fad ? > > God, I hope not -- or I'd rather be stupid. > > -- > Grant > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Linq to Python
Hi Roger I am impressed (i always suspected Python programmers are smart no doubt about it). But what about the case where they join different sources like the one here http://informationr.net/ir/13-2/TB0806.html Thanks for teaching me :-) i am thankful for that regards Hrishy > > names = ["Burke", "Connor", > "Frank", "Everett", > "Albert", "George", > "Harris", "David"] > > result = [each.upper() for each in names if len(each) == 5] > > result.sort() > > for each in result: print each > > > Yes clearly 'the Python crowd' must admit LINQ is > 'much better', I'm > sold, in fact off to download my "Free, but limited > editions of Visual > Studio 2005 for a single programming language supported by > .NET" right away! > > OK so maybe I'm being naive here but it looks to me > like this new > paradigm's big idea is to use a python + SQL type > syntax to access data > in random objects. Big whoop. It's not that difficult > to write a > generators that wraps XML files and databases is it? > > What am I missing here? > > > Roger Heathcote. > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Linq to Python
Hi Tim I am not a LINQ expert just a LINQ user and (was a little envious why the langauge i fantasize doesnt have it (pardon my ignorance of python)) LINQ as far as i know allows you to query all sources using a consistent interface . You can query a message queue ,xml document ,array object or relational source by learning LINQ and even join them as illustrated below http://informationr.net/ir/13-2/TB0806.html If somebody can tutor me how i can do that in python that would be great (and mayeb satisfy my greed and leave me with a happy feeling that my langauge python can do it) regards Hrishy --- On Wed, 24/9/08, Tim Golden <[EMAIL PROTECTED]> wrote: > From: Tim Golden <[EMAIL PROTECTED]> > Subject: Re: Linq to Python > To: > Cc: [email protected] > Date: Wednesday, 24 September, 2008, 8:20 PM > [EMAIL PROTECTED] wrote: > > sturlamolden: > >> No, because Python already has list comprehensions > and we don't need the XML buzzword.< > > > > LINQ is more than buzzwords. Python misses several of > those features. > > So maybe for once the Python crowd may recognize such > C# feature as > > much better than things present in Python. > > Said that, I presume Python will go on as usual, and > LINQ-like > > capabilities will not be integrated in Python. In the > meantime where I > > live lot of people will keep using C# instead of > Python and CLisp, > > natural selection at work indeed. > > Perhaps a quick summary of what LINQ offers which might > "be integrated into Python" would help those of > us who > are ignorant? (This is a serious comment; I'd like to > know). > > TJG > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Linq to Python
Hi Pardon my ignorance again but id ont see any join in python or did i miss something ? regards Hrishy --- On Thu, 25/9/08, sturlamolden <[EMAIL PROTECTED]> wrote: > From: sturlamolden <[EMAIL PROTECTED]> > Subject: Re: Linq to Python > To: [email protected] > Date: Thursday, 25 September, 2008, 12:02 PM > On 25 Sep, 12:06, hrishy <[EMAIL PROTECTED]> wrote: > > > [code] > > var stoogeGuys = > > Beginning with the XML source > > from xmlGuys in > xmlSource.Descendants("Stooge") > > Join to the array on the common element > "stoogeName" > > join arrayGuys in familyFacts > > on > xmlGuys.Element("stoogeName").Value equals > arrayGuys.stoogeName > > Join to the database on the common element > "stoogeName" > > join dbGuys in stoogeContext.stoogeTables > > on > xmlGuys.Element("stoogeName").Value equals > dbGuys.stoogeName > > select new > > { > > firstName = dbGuys.stoogeName, > > familyName = arrayGuys.familyName, > > birthDate = > xmlGuys.Element("birthDate").Value, > > deathDate = > xmlGuys.Element("deathDate").Value, > > hairCutStyle = dbGuys.stoogeHaircut, > > }; > > [/code] > > It could e.g. look like this in Python: > > stoogeGuys = [] > for xmlGuys in xmlSource.Descendants["Stooge"]: > arrayGuys = familyFacts[xmlGuys.stoogeName] > dbGuys = stoogeContext.stoogeTables[xmlGuys.stoogeName] > stoogeGuys += \ >[{'firstName':dbGuys.stoogeName, > 'familyName': arrayGuys.familyName, > 'birthDate':xmlGuys.birthDate, > 'deathDate':dbGuys.deathDate, > 'hairCutStyle': dbGuys.stoogeHaircut}] > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Linq to Python
Hi Thank you very much I appreciate taking the pain to explain this to me. regards Hrishy --- On Thu, 25/9/08, sturlamolden <[EMAIL PROTECTED]> wrote: > From: sturlamolden <[EMAIL PROTECTED]> > Subject: Re: Linq to Python > To: [email protected] > Date: Thursday, 25 September, 2008, 12:16 PM > On 25 Sep, 13:08, hrishy <[EMAIL PROTECTED]> wrote: > > > Pardon my ignorance again but id ont see any join in > python or did i miss something ? > > It's more Pythonic to use the syntax of dictionary > lookups. > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Sample code required to validate a xml file against XSD
Hi Does anybody have a python xample program to validate a xml file against a XSD. regards Hrisy -- http://mail.python.org/mailman/listinfo/python-list
lxml and schema validation
Hi
I am validating a xmlfile against a xsd (My.xsd) but i notice that the xsd has
a include which includes another xsd (My1.xsd)
I have written a simple program that to validate this
from lxml import etree
xmlschemadoc=etree.parse("My.xsd")
xmlschema=etree.XMLSchema(xmlschemadoc)
xmldoc=etree.parse("My.XML")
xmlschema.assertValid(xmldoc)
will my program validate against My.xsd and My1.xsd both ?
I also would like my program to continue validation against the xsd and not
stope at the first failure .
my question would be how do i do that in python ?
regards
Hrishy
--
http://mail.python.org/mailman/listinfo/python-list
Python book similar to Enterprise Recipes with Ruby and Rails
Hi Is there a python book that resemble this http://www.amazon.com/Enterprise-Recipes-Ruby-Rails-Schmidt/dp/1934356239/ref=sr_1_1?ie=UTF8&s=books&qid=1254914183&sr=8-1-spell Also is there a active record version or port of Python ? regards -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
Hi I am just a python beginner What you need is exceptions http://docs.python.org/tutorial/errors.html something on the lines of since you expect a integer and you wnat to catch the exception ... try: ... sum = 0; ... for item in readData: ...sum += int(item) ... except ValueError: ... print "Oops! That was no valid number. Try again...",item --- On Sun, 6/9/09, Maggie wrote: > From: Maggie > Subject: beginner's python help > To: [email protected] > Date: Sunday, 6 September, 2009, 8:00 AM > code practice: > > test = open ("test.txt", "r") > readData = test.readlines() > #set up a sum > sum = 0; > for item in readData: > sum += int(item) > print sum > > test file looks something like this: > > 34 > 23 > 124 > 432 > 12 > > when i am trying to compile this it gives me the error: > invalid > literal for int() with base 10 > > i know a lot of people get this and it usually means that > you try to > cast a string into an integer and this string does not > really contain > a “digit”..so I am just not sure how to correct it in > this case... > > thanks for your input > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
Hi Chris What if i want to log that bad data and continue processing is there a way to do that ? regards --- On Sun, 6/9/09, Chris Rebert wrote: > From: Chris Rebert > Subject: Re: beginner's python help > To: "Maggie" > Cc: [email protected] > Date: Sunday, 6 September, 2009, 8:15 AM > On Sun, Sep 6, 2009 at 12:00 AM, > Maggie > wrote: > > code practice: > > > > test = open ("test.txt", "r") > > readData = test.readlines() > > #set up a sum > > sum = 0; > > for item in readData: > > sum += int(item) > > print sum > > A slightly better way to write this: > > test = open("test.txt", "r") > #set up a sum > the_sum = 0 #avoid shadowing the built-in function sum() > for line in test: #iterate over the file directly instead > of reading > it into a list > the_sum += int(line) > print the_sum > > > test file looks something like this: > > > > 34 > > 23 > > 124 > > 432 > > 12 > > > > when i am trying to compile > > No, the error is happening at runtime. Pretty much only > SyntaxErrors > occur at compile-time. > > > this it gives me the error: invalid > > literal for int() with base 10 > > > > i know a lot of people get this and it usually means > that you try to > > cast a string into an integer and this string does not > really contain > > a “digit”..so I am just not sure how to correct it > in this case... > > I would recommend putting a `print repr(line)` inside the > loop, before > the "+=" line. This will show the input int() is getting so > you can > see out what the bad input is that's causing the error and > thus debug > the problem. > > Cheers, > Chris > -- > http://blog.rebertia.com > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: beginner's python help
Hi sum = 0 for item in readData: try: sum += int(item) except ValueError: print "Oops! That was no valid number. Instead it was:", item So you mean to say this would ignore the bad data and continue processing ? regards -- http://mail.python.org/mailman/listinfo/python-list
difficulty in understanding rsplit(None,1)[1]
Hi What does rsplit(None,1)[1] accomplish. Can somebody please decompose that to me. regards -- http://mail.python.org/mailman/listinfo/python-list
Re: difficulty in understanding rsplit(None,1)[1]
Hi Martin Many thanks And by the way great way to explain that thing --- On Tue, 22/9/09, Martin P. Hellwig wrote: > From: Martin P. Hellwig > Subject: Re: difficulty in understanding rsplit(None,1)[1] > To: [email protected] > Date: Tuesday, 22 September, 2009, 9:52 AM > hrishy wrote: > > Hi > > > > What does rsplit(None,1)[1] accomplish. > > > > Can somebody please decompose that to me. > > > > regards > > > Sure: > > >>> test = 'This is a test' > >>> help(test.rsplit) > Help on built-in function rsplit: > > rsplit(...) > S.rsplit([sep [,maxsplit]]) -> list of > strings > > Return a list of the words in the string S, > using sep as the > delimiter string, starting at the end of the > string and working > to the front. If maxsplit is given, at > most maxsplit splits are > done. If sep is not specified or is None, any > whitespace string > is a separator. > > >>> step_two = test.rsplit(None, 1) > >>> step_two > ['This is a', 'test'] > >>> > >>> step_two[1] > 'test' > >>> > > -- MPH > http://blog.dcuktec.com > 'If consumed, best digested with added seasoning to own > preference.' > -- http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
