Re: String slices

2019-08-12 Thread Rhodri James

On 10/08/2019 10:45, Peter J. Holzer wrote:

On 2019-08-10 09:10:12 +1000, Cameron Simpson wrote:

On 09Aug2019 22:28, Paul St George  wrote:

On 09/08/2019 16:29, Rhodri James wrote:

(Actually I would probably use outstream.write() and do my own
formatting, but let's not get side-tracked ;-)


I would love to hear your outstream.write() side-track!


I am not Rhodri James, but you can just write strings to text files:

  outstream.write("X: ")  # note, includes the space separator
  outstream.write(str(thing[0]))
  outstring.write("\n")


You can also format the string before passing it to write (which is
probably what Rhodri meant by "do my own formatting", like this:

 outstream.write("X: %7.2f\n" % thing[0])

or this

 outstream.write("X: {0:7.2f}\n".format(thing[0]))

or (since Python 3.6) this:

 outstream.write(f"X: {thing[0]:7.2f}\n")

There are of course many variants to all three methods.


That is indeed what I meant.  I do enough random dumping of things to 
files for debugging purposes that outstream.write() is second nature to 
me now.  I once even went as far as sys.stdout.write() before sanity 
caught up with me ;-)



--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: String slices

2019-08-10 Thread Paul St George


On 10/08/2019 17:35, Dennis Lee Bieber wrote:

On Sat, 10 Aug 2019 11:45:43 +0200, "Peter J. Holzer"
declaimed the following:



There are of course many variants to all three methods.

And then one can get downright nasty...


X = 3.14
Y = 2.78
Z = 6.226E23
print("".join(["Plane rotation %s: %s\n" % (nm, vl)

... for (nm, vl) in [("X", X), ("Y", Y), ("Z", Z)]]))
Plane rotation X: 3.14
Plane rotation Y: 2.78
Plane rotation Z: 6.226e+23

(replace "print" with "outstream.write" for use with the file)


|outstream.write| could be very useful, thank you Peter and Cameron 
(neither being Rhodri James). If I wanted to learn more about formatting 
strings is there a better place to go than:


https://docs.python.org/release/3.6.5/library/string.html?highlight=string#format-string-syntax

https://pyformat.info

https://python-reference.readthedocs.io/en/latest/docs/str/formatting.html

And Dennis, whatever you did there is very impressive and works 
perfectly but I don’t know enough to be able to use it. Please will you 
say more or direct me to some reference? I couldn’t find ‘nasty’ in the 
Python docs.

--
https://mail.python.org/mailman/listinfo/python-list


Re: String slices

2019-08-10 Thread Peter J. Holzer
On 2019-08-10 09:10:12 +1000, Cameron Simpson wrote:
> On 09Aug2019 22:28, Paul St George  wrote:
> > On 09/08/2019 16:29, Rhodri James wrote:
> > > (Actually I would probably use outstream.write() and do my own
> > > formatting, but let's not get side-tracked ;-)
> > > 
> > I would love to hear your outstream.write() side-track!
> 
> I am not Rhodri James, but you can just write strings to text files:
> 
>  outstream.write("X: ")  # note, includes the space separator
>  outstream.write(str(thing[0]))
>  outstring.write("\n")

You can also format the string before passing it to write (which is
probably what Rhodri meant by "do my own formatting", like this:

outstream.write("X: %7.2f\n" % thing[0])

or this

outstream.write("X: {0:7.2f}\n".format(thing[0]))

or (since Python 3.6) this:

outstream.write(f"X: {thing[0]:7.2f}\n")

There are of course many variants to all three methods.

hp

-- 
   _  | Peter J. Holzer| we build much bigger, better disasters now
|_|_) || because we have much more sophisticated
| |   | h...@hjp.at | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson 


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String slices

2019-08-09 Thread Cameron Simpson

On 09Aug2019 22:28, Paul St George  wrote:

On 09/08/2019 16:29, Rhodri James wrote:
The 'sep="\n"' parameter to print() means "put a newline between each 
item."  So don't do that.  Put the newlines you do want in explicitly, 
or use separate calls to print():


(I'm abbreviating because I really can't be bothered to type that 
much :-)


  print("X:", thing[0],
    "\nY:", thing[1],
    "\nZ:", thing[2],
    file=outstream)

or

  print("X:", thing[0], file=outstream)
  print("Y:", thing[1], file=outstream)
  print("Z:", thing[2], file=outstream)

I would probably use the latter, but it's just a matter of personal 
preference.


(Actually I would probably use outstream.write() and do my own 
formatting, but let's not get side-tracked ;-)


So, I am going with your second suggestion (see below) but I would love 
to hear your outstream.write() side-track!


I am not Rhodri James, but you can just write strings to text files:

 outstream.write("X: ")  # note, includes the space separator
 outstream.write(str(thing[0]))
 outstring.write("\n")

print() is convenient, because it calls str() on any non-str argument 
for you, and it does the space separators and newline line ending (both 
overridable).


You can see the above is rather verbose. But it does let you control 
exactly what gets written, _if_ the print separators and endings are 
causing you inconvenience.


Personally, for text output, I use print unless there's some compelling 
reason not to (such as transcribing a data structure precisely, not 
wanting unexpected spaces and newlines; if I were hand transcribing JSON 
or XML or HTML or the like).


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: String slices

2019-08-09 Thread Paul St George


On 09/08/2019 16:29, Rhodri James wrote:

On 09/08/2019 15:13, Paul St George wrote:

In the code (below) I want a new line like this:

Plane rotation X: 0.0
Plane rotation Y: 0.0
Plane rotation Z: 0.0

But not like this:

Plane rotation X:
0.0
Plane rotation Y:
0.0
Plane rotation Z:
0.0

Is it possible?
(I am using Python 3.5 within Blender.)

#
import os

outstream = open(os.path.splitext(bpy.data.filepath)[0] + ".txt",'w')

print(

"Plane rotation X:",bpy.data.objects["Plane"].rotation_euler[0],

"Plane rotation Y:",bpy.data.objects["Plane"].rotation_euler[1],

"Plane rotation Z:",bpy.data.objects["Plane"].rotation_euler[2],

file=outstream, sep="\n"

)

outstream.close()


The 'sep="\n"' parameter to print() means "put a newline between each 
item."  So don't do that.  Put the newlines you do want in explicitly, 
or use separate calls to print():


(I'm abbreviating because I really can't be bothered to type that much 
:-)


  print("X:", thing[0],
    "\nY:", thing[1],
    "\nZ:", thing[2],
    file=outstream)

or

  print("X:", thing[0], file=outstream)
  print("Y:", thing[1], file=outstream)
  print("Z:", thing[2], file=outstream)

I would probably use the latter, but it's just a matter of personal 
preference.


(Actually I would probably use outstream.write() and do my own 
formatting, but let's not get side-tracked ;-)





So, I am going with your second suggestion (see below) but I would love 
to hear your outstream.write() side-track!



import os

with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", "w") as outstream:

   plane = bpy.data.objects["Plane"]

   print("Plane rotation X:",plane.rotation_euler[0], file=outstream)

   print("Plane rotation Y:",plane.rotation_euler[1], file=outstream)

   print("Plane rotation Z:",plane.rotation_euler[2], file=outstream)

   print("Focal length:", bpy.context.object.data.lens, file=outstream)

   and so on...




--
https://mail.python.org/mailman/listinfo/python-list


Re: String slices

2019-08-09 Thread Peter Otten
Paul St George wrote:

> In the code (below) I want a new line like this:
> 
> Plane rotation X: 0.0
> Plane rotation Y: 0.0
> Plane rotation Z: 0.0
> 
> But not like this:
> 
> Plane rotation X:
> 0.0
> Plane rotation Y:
> 0.0
> Plane rotation Z:
> 0.0
> 
> Is it possible?

> print(
> 
> "Plane rotation X:",bpy.data.objects["Plane"].rotation_euler[0],
> 
> "Plane rotation Y:",bpy.data.objects["Plane"].rotation_euler[1],
> 
> "Plane rotation Z:",bpy.data.objects["Plane"].rotation_euler[2],
> 
> file=outstream, sep="\n"
> 
> )

Explanation: The newlines between all positional args of print() are caused 
by the sep="\n" keyword argument. 

You probably got the idea from my

x, y, z = bpy.data.objects["Plane"].rotation_euler
print(
"Plane rotation X:", x,
"Plane rotation Y:", y,
"Plane rotation Z:", z,
file=outstream, sep="\n"
)

in the other thread where I introduced the bug when trying to simplify my 
original suggestion (which I didn't post)

print(
f"Plane rotation X: {x}",
f"Plane rotation Y: {y}",
f"Plane rotation Z: {z}",
file=outstream, sep="\n"
)

But as Dan says, use separate print() calls.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String slices

2019-08-09 Thread Rhodri James

On 09/08/2019 15:13, Paul St George wrote:

In the code (below) I want a new line like this:

Plane rotation X: 0.0
Plane rotation Y: 0.0
Plane rotation Z: 0.0

But not like this:

Plane rotation X:
0.0
Plane rotation Y:
0.0
Plane rotation Z:
0.0

Is it possible?
(I am using Python 3.5 within Blender.)

#
import os

outstream = open(os.path.splitext(bpy.data.filepath)[0] + ".txt",'w')

print(

"Plane rotation X:",bpy.data.objects["Plane"].rotation_euler[0],

"Plane rotation Y:",bpy.data.objects["Plane"].rotation_euler[1],

"Plane rotation Z:",bpy.data.objects["Plane"].rotation_euler[2],

file=outstream, sep="\n"

)

outstream.close()


The 'sep="\n"' parameter to print() means "put a newline between each 
item."  So don't do that.  Put the newlines you do want in explicitly, 
or use separate calls to print():


(I'm abbreviating because I really can't be bothered to type that much :-)

  print("X:", thing[0],
"\nY:", thing[1],
"\nZ:", thing[2],
file=outstream)

or

  print("X:", thing[0], file=outstream)
  print("Y:", thing[1], file=outstream)
  print("Z:", thing[2], file=outstream)

I would probably use the latter, but it's just a matter of personal 
preference.


(Actually I would probably use outstream.write() and do my own 
formatting, but let's not get side-tracked ;-)





--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: String slices

2019-08-09 Thread Dan Sommers

On 8/9/19 10:13 AM, Paul St George wrote:

In the code (below) I want a new line like this:

Plane rotation X: 0.0
Plane rotation Y: 0.0
Plane rotation Z: 0.0

But not like this:

Plane rotation X:
0.0
Plane rotation Y:
0.0
Plane rotation Z:
0.0

Is it possible?
(I am using Python 3.5 within Blender.)

#
import os

outstream = open(os.path.splitext(bpy.data.filepath)[0] + ".txt",'w')

print(

"Plane rotation X:",bpy.data.objects["Plane"].rotation_euler[0],

"Plane rotation Y:",bpy.data.objects["Plane"].rotation_euler[1],

"Plane rotation Z:",bpy.data.objects["Plane"].rotation_euler[2],

file=outstream, sep="\n"

)

outstream.close()



Use separate calls to print:

with open(...) as outstream:
plane = bpy.data.objects["Plane"]
print("X:", plane.rotation_euler[0])
print("Y:", plane.rotation_euler[1])
# print Z here

And please (a) use "with" instead of "open" and "close," and
(b) use an email client that preserves indentation.
--
https://mail.python.org/mailman/listinfo/python-list


RE: String slices work only for first string character ?

2008-12-16 Thread Barak, Ron
Thanks to all who pointed my wrong understanding of how string slices are 
defined.
Bye,
Ron.


From: Barak, Ron [mailto:ron.ba...@lsi.com]
Sent: Tuesday, December 16, 2008 15:35
To: python-list@python.org
Subject: String slices work only for first string character ?


Hi,

Can any one explain why the following string slice works only for the first 
character, but not for any other ?

$ cat /tmp/tmp.py
#!/usr/bin/env python

data = 'F0023209006-0101'
print data
print "|"+data[0:1]+"|"
print "|"+data[1:1]+"|"
print "|"+data[2:1]+"|"

$ python `cygpath -w /tmp/tmp.py`
F0023209006-0101
|F|
||
||

$

Thanks,
Ron.
--
http://mail.python.org/mailman/listinfo/python-list


RE: String slices work only for first string character ?

2008-12-16 Thread Barak, Ron
Hi Mr. Cain,
Mae culpa: obviously, I erroneously understood the number after the ':' as the 
string length.
Thanks,
Ron.

-Original Message-
From: D'Arcy J.M. Cain [mailto:da...@druid.net]
Sent: Tuesday, December 16, 2008 15:45
To: Barak, Ron
Cc: python-list@python.org
Subject: Re: String slices work only for first string character ?

On Tue, 16 Dec 2008 13:35:27 +
"Barak, Ron"  wrote:
> Can any one explain why the following string slice works only for the first 
> character, but not for any other ?

I think that you need to reread the docs on slices.

> print "|"+data[0:1]+"|"
> print "|"+data[1:1]+"|"

If you want the second character use "data[1:2]".

--
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
--
http://mail.python.org/mailman/listinfo/python-list


Re: String slices work only for first string character ?

2008-12-16 Thread D'Arcy J.M. Cain
On Tue, 16 Dec 2008 13:35:27 +
"Barak, Ron"  wrote:
> Can any one explain why the following string slice works only for the first 
> character, but not for any other ?

I think that you need to reread the docs on slices.

> print "|"+data[0:1]+"|"
> print "|"+data[1:1]+"|"

If you want the second character use "data[1:2]".

-- 
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
--
http://mail.python.org/mailman/listinfo/python-list


Re: String slices work only for first string character ?

2008-12-16 Thread Tim Chase

Can any one explain why the following string slice works only for the first 
character, but not for any other ?

$ cat /tmp/tmp.py
#!/usr/bin/env python

data = 'F0023209006-0101'
print data
print "|"+data[0:1]+"|"
print "|"+data[1:1]+"|"
print "|"+data[2:1]+"|"

$ python `cygpath -w /tmp/tmp.py`
F0023209006-0101
|F|
||
||


Slices are defined by [start_idx:end_idx] not [start_idx:length] 
so you want


  data[0:1]
  data[1:2]
  data[2:3]

-tkc



--
http://mail.python.org/mailman/listinfo/python-list