[Tutor] HELP! How do I remove the black after "s="

2014-05-10 Thread 1 2
In the result it shows "s= 8" pls tell me how to remove the blank?

s,t,n = 0,0,1
while t <= s:
s,t,n = s+2,t+n,n+1
else:
print('s=',s,n)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] HELP! How do I remove the black after "s="

2014-05-10 Thread Chris “Kwpolska” Warrick
On Sat, May 10, 2014 at 8:35 AM, 1 2  wrote:
> In the result it shows "s= 8" pls tell me how to remove the blank?
>
> s,t,n = 0,0,1
> while t <= s:
> s,t,n = s+2,t+n,n+1
> else:
> print('s=',s,n)

You must use something else.  For example:

print('s={0} {1}'.format(s, n))

This will produce "s=8 5".  If you want "s=85", remove the space in
the format string.

-- 
Chris “Kwpolska” Warrick 
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] HELP! How do I remove the black after "s="

2014-05-10 Thread Alan Gauld

On 10/05/14 07:35, 1 2 wrote:

In the result it shows "s= 8" pls tell me how to remove the blank?

s,t,n = 0,0,1
while t <= s:
 s,t,n = s+2,t+n,n+1
else:
 print('s=',s,n)


Assuming you are using Python version 3 you need to specify the sep 
option to print:


print('s=',s,n,sep='')

>>> help(print)
Help on built-in function print in module builtins:

print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep:  string inserted between values, default a space.
end:  string appended after the last value, default a newline.
(END)

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


Re: [Tutor] xml parsing from xml

2014-05-10 Thread Stefan Behnel
Danny Yoo, 07.05.2014 22:39:
> If you don't want to deal with a event-driven approach that SAX
> emphasizes, you may still be able to do this problem with an XML-Pull
> parser.  You mention that your input is hundreds of megabytes long, in
> which case you probably really do need to be careful about memory
> consumption.  See:
> 
> https://wiki.python.org/moin/PullDom

Since the OP mentioned that the file is quite large (800 MB), not only
memory consumption should matter but also processing time. If that is the
case, PullDOM isn't something to recommend since it's MiniDOM based, which
makes it quite slow overall.

Stefan


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


Re: [Tutor] HELP! How do I remove the black after "s="

2014-05-10 Thread Steven D'Aprano
Hello "1 2", and welcome!

(By the way, I feel quite silly calling you by the name you show in your 
email address. Do you have another name you would prefer to be known 
by?)

My response below.


On Sat, May 10, 2014 at 02:35:07PM +0800, 1 2 wrote:
> In the result it shows "s= 8" pls tell me how to remove the blank?

There are a few different ways. The easiest is to tell the print() 
function not to use a space as seperator between terms. (This only works 
in Python 3.)

py> print("s=", 8, 9)
s= 8 9
py> print("s=", 8, 9, sep='')
s=89

But note that the separator applies between all the items, so that's not 
very useful in your case. Instead, you can build up a string, then print 
the string. Here are two different ways to do it, the first may be 
familiar with you if you know how to program in C.

py> print("s=%s %s" % (8, 9))
s=8 9
py> print("s={} {}".format(8, 9))
s=8 9


Hope this helps.


-- 
Steven

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


Re: [Tutor] xml parsing from xml

2014-05-10 Thread Stefan Behnel
Stefan Behnel, 10.05.2014 10:57:
> Danny Yoo, 07.05.2014 22:39:
>> If you don't want to deal with a event-driven approach that SAX
>> emphasizes, you may still be able to do this problem with an XML-Pull
>> parser.  You mention that your input is hundreds of megabytes long, in
>> which case you probably really do need to be careful about memory
>> consumption.  See:
>>
>> https://wiki.python.org/moin/PullDom
> 
> Since the OP mentioned that the file is quite large (800 MB), not only
> memory consumption should matter but also processing time. If that is the
> case, PullDOM isn't something to recommend since it's MiniDOM based, which
> makes it quite slow overall.

To back that by some numbers, here are three memory efficient
implementations, using PullDOM, cElementTree and lxml.etree:


$ cat lx.py
from lxml.etree import iterparse, tostring

doc = iterparse('input.xml', tag='country')
root = None
for _, node in doc:
print("--")
print("This is the node for " + node.get('name'))
print("--")
print(tostring(node))
print("\n\n")

if root is None:
root = node.getparent()
else:
sib = node.getprevious()
if sib is not None:
root.remove(sib)

$ cat et.py
from xml.etree.cElementTree import iterparse, tostring

doc = iterparse('input.xml')
for _, node in doc:
if node.tag == "country":
print("--")
print("This is the node for " + node.get('name'))
print("--")
print(tostring(node))
print("\n\n")
node.clear()

$ cat pdom.py
from xml.dom.pulldom import START_ELEMENT, parse

doc = parse('input.xml')
for event, node in doc:
if event == START_ELEMENT and node.localName == "country":
doc.expandNode(node)
print("--")
print("This is the node for " + node.getAttribute('name'))
print("--")
print(node.toxml())
print("\n\n")


I ran all three against a 400 MB XML file generated by repeating the data
snippet the OP provided. Here are the system clock timings in
minutes:seconds, on 64bit Linux, using CPython 3.4.0:

$ time python3 lx.py > /dev/null
time: 0:31
$ time python3 et.py > /dev/null
time: 3:33
$ time python3 pdom.py > /dev/null
time: 9:51


Adding to that another bit of actual tree processing, if I had to choose
between 2 minutes and well over 20 minutes processing time for my 800MB,
I'd tend to prefer the 2 minutes.

Note that the reason why cElementTree performs so poorly here is that its
serialiser is fairly slow, and the code writes the entire 400 MB of XML
back out. If the test was more like "parse 400 MB and generate CSV from
it", then it should perform similar to lxml. PullDOM/MiniDOM, on the other
hand, are slow on parsing, serialisation *and* tree processing.

Stefan


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