Re: [Tutor] newb help reading lines from csv

2012-10-26 Thread Matt Williams

Dear Rob,

This caught me out as well for a long time.

As I understand it, csv.reader is a file-reader, which iterates ONCE 
over the file. There may be more elegant solutions, but I do:


import csv
ifile = open('test.csv', r)
reader = csv.reader(ifile)
inData = []
for row in reader:
inData.append[row]  
ifile.close()

you can now loop through inData to your heart's desire.

HTH,

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


[Tutor] newb help reading lines from csv

2012-10-22 Thread Dewhirst, Rob
import csv
ifile = open('test.csv', r)
reader = csv.reader(ifile)
for row in reader:
print row
for row in reader:
print row
ifile.close()

This is a simplified version of what I am trying to do - loop through
a CSV file twice.  Why does the second for loop not execute at all?
The first one prints the rows of the file just fine.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread LZAntal
Hi,

On Oct 22, 2012, at 12:20 PM, Dewhirst, Rob robdewhi...@gmail.com wrote:

 import csv
 ifile = open('test.csv', r)
 reader = csv.reader(ifile)
 for row in reader:
   print row
 for row in reader:
   print row
 ifile.close()
 
 This is a simplified version of what I am trying to do - loop through
 a CSV file twice.  Why does the second for loop not execute at all?
 The first one prints the rows of the file just fine.
 _

I believe csv module uses iterator so you need to run reader.seek(0) between 
the for loops


Laszlo
http://twitter.com/LZAntal


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

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


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread Emile van Sebille

Dewhirst, Rob wrote:

import csv
ifile = open('test.csv', r)
reader = csv.reader(ifile)
for row in reader:
print row
for row in reader:
print row
ifile.close()

This is a simplified version of what I am trying to do - loop through
a CSV file twice.  Why does the second for loop not execute at all?
The first one prints the rows of the file just fine.


The first pass also exhausts the input feed -- you'll need to rewind or 
reposition the next line pointer to the start of the file and I suspect 
the easiest way is to reopen the file.


Emile

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


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread Joel Goldstick
On Mon, Oct 22, 2012 at 3:20 PM, Dewhirst, Rob robdewhi...@gmail.com wrote:
 import csv
 ifile = open('test.csv', r)
 reader = csv.reader(ifile)
 for row in reader:
 print row
 for row in reader:
 print row
 ifile.close()

 This is a simplified version of what I am trying to do - loop through
 a CSV file twice.  Why does the second for loop not execute at all?
 The first one prints the rows of the file just fine.
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

You need to close the file and re open to iterate again.  You can't
reset the iterator back to the beginning without closing and
re-opening

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


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread Dewhirst, Rob
Thanks Matt and Lazlo.  I knew I must have been missing some counter
not being reset.  Both of those options work fine.

You want to do ifile.seek(0) not reader.seek(0) at least from my testing.


On Mon, Oct 22, 2012 at 2:27 PM, Matt Williams m...@doctors.org.uk wrote:
 Dear Rob,

 This caught me out as well for a long time.

 As I understand it, csv.reader is a file-reader, which iterates ONCE over
 the file. There may be more elegant solutions, but I do:


 import csv
 ifile = open('test.csv', r)
 reader = csv.reader(ifile)
 inData = []
 for row in reader:
 inData.append[row]
 ifile.close()

 you can now loop through inData to your heart's desire.

 HTH,

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


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread eryksun
On Mon, Oct 22, 2012 at 3:28 PM, LZAntal lzan...@gmail.com wrote:
 On Oct 22, 2012, at 12:20 PM, Dewhirst, Rob robdewhi...@gmail.com wrote:

 import csv
 ifile = open('test.csv', r)
 reader = csv.reader(ifile)

 I believe csv module uses iterator so you need to run reader.seek(0) between 
 the for loops

You have to reset the file iterator with ifile.seek(0). You might also
want a new reader if you care about the line_num attribute, but
otherwise you can keep using the same reader.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread Joel Goldstick
On Mon, Oct 22, 2012 at 3:35 PM, Dewhirst, Rob robdewhi...@gmail.com wrote:
 Thanks Matt and Lazlo.  I knew I must have been missing some counter
 not being reset.  Both of those options work fine.

 You want to do ifile.seek(0) not reader.seek(0) at least from my testing.


 On Mon, Oct 22, 2012 at 2:27 PM, Matt Williams m...@doctors.org.uk wrote:
 Dear Rob,

 This caught me out as well for a long time.

 As I understand it, csv.reader is a file-reader, which iterates ONCE over
 the file. There may be more elegant solutions, but I do:


 import csv
 ifile = open('test.csv', r)
 reader = csv.reader(ifile)
 inData = []
 for row in reader:
 inData.append[row]
 ifile.close()

 you can now loop through inData to your heart's desire.

 HTH,

 Matt


If you want to learn more about python iterators this is a good place
to start: http://docs.python.org/howto/functional.html#iterators
or google python iterators.  They are pretty cool to understand
-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread eryksun
On Mon, Oct 22, 2012 at 3:35 PM, Dewhirst, Rob robdewhi...@gmail.com wrote:

 import csv
 ifile = open('test.csv', r)
 reader = csv.reader(ifile)
 inData = []
 for row in reader:
 inData.append[row]
 ifile.close()

 you can now loop through inData to your heart's desire.

Alternatively:

import csv
with open('test.csv', 'rb') as ifile:
inData = list(csv.reader(ifile))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread Prasad, Ramit
Dewhirst, Rob wrote: 
 Thanks Matt and Lazlo.  I knew I must have been missing some counter
 not being reset.  Both of those options work fine.
 
 You want to do ifile.seek(0) not reader.seek(0) at least from my testing.
 
 
 On Mon, Oct 22, 2012 at 2:27 PM, Matt Williams m...@doctors.org.uk wrote:
[snip]

Please do not top post. This list's etiquette is to write your 
response in-line or below the quoted text. Thank you.

Ramit Prasad

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb Learning Question

2008-04-03 Thread Alan Gauld

bob gailer [EMAIL PROTECTED] wrote 

 3rd alternative: if c  'm': print c

Wow! Amazingly I just assumed you couldn't directly 
compare characters with boolean tests. I don't know 
why I thought that since it must be possible for string 
comparisons, but I did.

You learn something new every day, :-)

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb Learning Question

2008-04-03 Thread Jeffrey Dates

  3rd alternative: if c  'm': print c


after futzing around with interpreting the problem, I ended up with a
solution that utilizes the character comparison.

Write a program that prints the first letter of a string that comes after
'm' in the alphabet.

s = this is my string
for i in s:
  if i  'm':
print i
break


Thanks for all the help everyone.  Seems so obvious now.. ;-)

I'm sure I'll be back with some more newb questions in the future. ;-)

jeff.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb Learning Question

2008-04-03 Thread Sander Sweers
Should be replying to the listSorry :(

On Thu, Apr 3, 2008 at 11:14 AM, Sander Sweers [EMAIL PROTECTED] wrote:
 On Thu, Apr 3, 2008 at 8:21 AM, Jeffrey Dates [EMAIL PROTECTED] wrote:
 3rd alternative: if c  'm': print c

  snip


   Write a program that prints the first letter of a string that comes after
   'm' in the alphabet.
  
   s = this is my string
   for i in s:
 if i  'm':
   print i
   break

  One potential problem you will have is with upper case letters.

   'N'  'm'
  False

  To make input all lower case use .lower()

   'N'.lower()  'm'
  True

  Greets
  Sander

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb Learning Question

2008-04-03 Thread Jeffrey Dates
Exellent suggestion!
thanks!



On Thu, Apr 3, 2008 at 2:17 PM, Sander Sweers [EMAIL PROTECTED]
wrote:

 Should be replying to the listSorry :(

 On Thu, Apr 3, 2008 at 11:14 AM, Sander Sweers [EMAIL PROTECTED]
 wrote:
  On Thu, Apr 3, 2008 at 8:21 AM, Jeffrey Dates [EMAIL PROTECTED]
 wrote:
  3rd alternative: if c  'm': print c
 
   snip
 
 
Write a program that prints the first letter of a string that comes
 after
'm' in the alphabet.
   
s = this is my string
for i in s:
  if i  'm':
print i
break
 
   One potential problem you will have is with upper case letters.
 
'N'  'm'
   False
 
   To make input all lower case use .lower()
 
'N'.lower()  'm'
   True
 
   Greets
   Sander
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Newb Learning Question

2008-04-02 Thread Jeffrey Dates
Greetings,

I have no previous experience in scripting.  Python is my first language...
I'm currently trying to build a logic model for how to 'think' in Python, so
please excuse my ignorance.

Currently, I'm running through some basic 101 tutorials, to wrap my head
around solving problems.
I'm looking for some advice on how to solve the following exercise:

Write a program that prints the first letter of a string that comes after
'm' in the alphabet.

I hope this is not too elementary.

I do understand basic, basic concepts.  iteration, for loops, etc.
Just having trouble with syntax, and how to format my logic.

thank you in advance for any help or examples.

Jeffrey Dates
www.kungfukoi.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb Learning Question

2008-04-02 Thread Jeffrey Dates
Sorry forgot to post what I had so far:

for code in range(ord(a), ord(z) +1):
if code == ord(m):
print chr(code +1)

Now, this solves for the first letter after M, but doesn't do it via a
string

thanks,

Jeffrey Dates
www.kungfukoi.com






On Wed, Apr 2, 2008 at 12:44 PM, Jeffrey Dates [EMAIL PROTECTED] wrote:

 Greetings,

 I have no previous experience in scripting.  Python is my first
 language...
 I'm currently trying to build a logic model for how to 'think' in Python,
 so please excuse my ignorance.

 Currently, I'm running through some basic 101 tutorials, to wrap my head
 around solving problems.
 I'm looking for some advice on how to solve the following exercise:

 Write a program that prints the first letter of a string that comes after
 'm' in the alphabet.

 I hope this is not too elementary.

 I do understand basic, basic concepts.  iteration, for loops, etc.
 Just having trouble with syntax, and how to format my logic.

 thank you in advance for any help or examples.

 Jeffrey Dates
 www.kungfukoi.com






___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb Learning Question

2008-04-02 Thread taserian
On Wed, Apr 2, 2008 at 12:50 PM, Jeffrey Dates [EMAIL PROTECTED] wrote:

 Sorry forgot to post what I had so far:

 for code in range(ord(a), ord(z) +1):
 if code == ord(m):
 print chr(code +1)

 Now, this solves for the first letter after M, but doesn't do it via a
 string

 thanks,


Let's think about the problem in pseudocode:

If the first letter in the string comes after m in the alphabet, print that
letter.
Otherwise, if the second letter comes after m in the alphabet, print *that*
letter.
etc. etc.

So what you want is to *iterate* through the characters that make up the
string (from start to end) until you find a character that comes after m.

Your code above seems to *iterate* through all the characters from a to
z, not the characters of a given string.

See if you can fix that first, then we'll talk about the next step.

Tony R.
aka Taser



 Jeffrey Dates
 www.kungfukoi.com






   On Wed, Apr 2, 2008 at 12:44 PM, Jeffrey Dates [EMAIL PROTECTED]
 wrote:

  Greetings,
 
  I have no previous experience in scripting.  Python is my first
  language...
  I'm currently trying to build a logic model for how to 'think' in
  Python, so please excuse my ignorance.
 
  Currently, I'm running through some basic 101 tutorials, to wrap my head
  around solving problems.
  I'm looking for some advice on how to solve the following exercise:
 
  Write a program that prints the first letter of a string that comes
  after 'm' in the alphabet.
 
  I hope this is not too elementary.
 
  I do understand basic, basic concepts.  iteration, for loops, etc.
  Just having trouble with syntax, and how to format my logic.
 
  thank you in advance for any help or examples.
 
  Jeffrey Dates
  www.kungfukoi.com
 
 
 
 
 
 

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb Learning Question

2008-04-02 Thread bob gailer
Jeffrey Dates wrote:
 Greetings,

 I have no previous experience in scripting.  Python is my first 
 language...
 I'm currently trying to build a logic model for how to 'think' in 
 Python, so please excuse my ignorance.

 Currently, I'm running through some basic 101 tutorials, to wrap my 
 head around solving problems.
 I'm looking for some advice on how to solve the following exercise:

 Write a program that prints the first letter of a string that comes 
 after 'm' in the alphabet.

 what I had so far:

 for code in range(ord(a), ord(z) +1):
 if code == ord(m):
 print chr(code +1)

 Now, this solves for the first letter after M

which is NOT what the exercise wants!

Consider are you ready?. Which character is the first that comes after 
'm' in the alphabet.?

BTW did you mean after m? Caps and lower case are different.

 , but doesn't do it via a string

I for one am reluctant to just give an answer. I prefer to lead you to a 
solution.

So, I suggest you write a program to:
assign a candidate string to a variable (choose a string that has at 
least one letter that comes after 'm')
test each character to see if it comes after 'm'
print that character
stop

Do as much as you can, and ask more questions.

-- 
Bob Gailer
919-636-4239 Chapel Hill, NC

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb Learning Question

2008-04-02 Thread Jeffrey Dates
That's Bob, and Tony,  exactly what I'm looking for. ( not the answer, but
the path to it... )

Let me get back to you with my result after I study this a bit.
thanks!!

Jeffrey Dates
www.kungfukoi.com




On undefined, bob gailer [EMAIL PROTECTED] wrote:

 Jeffrey Dates wrote:
  Greetings,
 
  I have no previous experience in scripting.  Python is my first
  language...
  I'm currently trying to build a logic model for how to 'think' in
  Python, so please excuse my ignorance.
 
  Currently, I'm running through some basic 101 tutorials, to wrap my
  head around solving problems.
  I'm looking for some advice on how to solve the following exercise:
 
  Write a program that prints the first letter of a string that comes
  after 'm' in the alphabet.
 
  what I had so far:
 
  for code in range(ord(a), ord(z) +1):
  if code == ord(m):
  print chr(code +1)
 
  Now, this solves for the first letter after M

 which is NOT what the exercise wants!

 Consider are you ready?. Which character is the first that comes after
 'm' in the alphabet.?

 BTW did you mean after m? Caps and lower case are different.

  , but doesn't do it via a string

 I for one am reluctant to just give an answer. I prefer to lead you to a
 solution.

 So, I suggest you write a program to:
assign a candidate string to a variable (choose a string that has at
 least one letter that comes after 'm')
test each character to see if it comes after 'm'
print that character
stop

 Do as much as you can, and ask more questions.

 --
 Bob Gailer
 919-636-4239 Chapel Hill, NC


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb Learning Question

2008-04-02 Thread Alan Gauld

Jeffrey Dates [EMAIL PROTECTED] wrote

 Let me get back to you with my result after I study this a bit.
 thanks!!

One wee tip you might find useful.

To test if a lertter comes after 'm you could

a) create a string with all letters after m

 after_m = 'nopqrstuvwxyz'

then test whether your characters were in after_m:

 if c in after_m: print c

OR

b) see if the ascii value of your character is bigger 
than the ascii value of 'm' And you can check the 
ascii value using ord()

There are pros and cons to both approaches. 
Pick the one you like best, we can debate 
the ideal solution for any given case later...

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb Learning Question

2008-04-02 Thread bob gailer
Alan Gauld wrote:
 Jeffrey Dates [EMAIL PROTECTED] wrote

   
 Let me get back to you with my result after I study this a bit.
 thanks!!
 

 One wee tip you might find useful.

 To test if a lertter comes after 'm you could

 a) create a string with all letters after m

   
 after_m = 'nopqrstuvwxyz'
 

 then test whether your characters were in after_m:

   
 if c in after_m: print c
 

 OR

 b) see if the ascii value of your character is bigger 
 than the ascii value of 'm' And you can check the 
 ascii value using ord()
   

3rd alternative: if c  'm': print c
 There are pros and cons to both approaches. 
 Pick the one you like best, we can debate 
 the ideal solution for any given case later...

   


-- 
Bob Gailer
919-636-4239 Chapel Hill, NC

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb - Problem reading binary files

2007-05-12 Thread Kent Johnson
Elizabeth Finn wrote:
 I need to read a file that is in binary format, and then convert some of 
 the values into integer values. These values can range from 1 to 4 bytes.
  
 First question – is there an easy way to do this? I finally wrote my own 
 little utility to handle multi-byte integers because I couldn’t find a 
 built-in way (except for ord() which works only for single bytes)

Alan suggested the struct module. It doesn't directly support 3-byte 
integers but perhaps you could pad them with a zero byte at the front. 
struct does let you unpack a whole string at once.

The Construct library is a higher-level interface to similar 
functionality. It doesn't seem to support 3-byte integers either but it 
is extensible.
  
 def getnum(num_str):
 
 Given a string representing a binary number, return the number.
 If string is more than one byte, calculate the number to return.
 Assume that each byte is signed magnitude

 x = len(num_str)
 ans = 0
 for i in range( x ):
 nextstr = num_str[i:i+1]
 ans = ans * 256
 ans = ans + ord(nextstr)
 return ans

Your loop could be written more simply as
for nextstr in num_str:
   ans = ans * 256
   ans = ans + ord(nextstr)

 This “brute force” method usually works, but - now here is the other 
 question -sometimes the code does not pick up two full bytes when it is 
 supposed to. I open the file and read a block that I want into a string:

 f=open(fname, 'rb')
 f.seek(offset, 0)
 block = f.read(2000)
  
 Then for each number I pull the bytes from the string, then call 
 getnum() to calculate the number.
  
 test = block[0:1] # 1 byte
 test = block[1:4] # 3 bytes
 test = block[4:6] # 2 bytes
 test = block[20:12] # 2 bytes
 test = block[1996:2000]   #4 bytes
  
 This method usually works, except that for some of the 2-byte numbers I 
 get only the first byte and first half of the second byte – for 
 instance: 'x06\x33’ comes out as ‘x063’. This is very confusing 
 especially because one 2-byte substring – “00 01” comes out as expected, 
 but “06 52” becomes “065”. Any ideas?

It seems to work for me. I wonder if you are confused about the input 
you are giving it? Using your definition of getnum(), I get these results:
In [31]: getnum('\x06\x33')
Out[31]: 1587
In [33]: 6*256 + 0x33
Out[33]: 1587

In [34]: getnum('\x06\x52')
Out[34]: 1618
In [35]: 6*256 + 0x52
Out[35]: 1618

So it seems to be doing the right thing. Can you put
   print repr(test)
   print getnum(test)
into your test program and show us the results?

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] newb - Problem reading binary files

2007-05-11 Thread Elizabeth Finn
This is probably a newbie question, and I apologize for the length – but I have 
consulted several books / sites and haven’t found a good answer. I need to read 
a file that is in binary format, and then convert some of the values into 
integer values. These values can range from 1 to 4 bytes.
   
  First question – is there an easy way to do this? I finally wrote my own 
little utility to handle multi-byte integers because I couldn’t find a built-in 
way (except for ord() which works only for single bytes)
   
  def getnum(num_str):
  
  Given a string representing a binary number, return the number.
  If string is more than one byte, calculate the number to return.
  Assume that each byte is signed magnitude
  
  x = len(num_str)
  ans = 0
  for i in range( x ):
  nextstr = num_str[i:i+1]
  ans = ans * 256
  ans = ans + ord(nextstr)
  return ans
   
  This “brute force” method usually works, but - now here is the other question 
-sometimes the code does not pick up two full bytes when it is supposed to. I 
open the file and read a block that I want into a string:
  
  f=open(fname, 'rb')
  f.seek(offset, 0)
  block = f.read(2000)
   
  Then for each number I pull the bytes from the string, then call getnum() to 
calculate the number.
   
  test = block[0:1] # 1 byte 
  test = block[1:4] # 3 bytes
  test = block[4:6] # 2 bytes
  test = block[20:12] # 2 bytes
  test = block[1996:2000]   #4 bytes
   
  This method usually works, except that for some of the 2-byte numbers I get 
only the first byte and first half of the second byte – for instance: 'x06\x33’ 
comes out as ‘x063’. This is very confusing especially because one 2-byte 
substring – “00 01” comes out as expected, but “06 52” becomes “065”. Any ideas?

 
-
Finding fabulous fares is fun.
Let Yahoo! FareChase search your favorite travel sites to find flight and hotel 
bargains.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb - Problem reading binary files

2007-05-11 Thread Alan Gauld

Elizabeth Finn [EMAIL PROTECTED] wrote 

 binary format, and then convert some of the values into 
 integer values. These values can range from 1 to 4 bytes.

See the struct module, and for an exampole the binary file 
sidebar in the file handling topic of my tutor. But caveat: 
struct needs to know the sizes and types of data you are 
reading in advance.
   
  for some of the 2-byte numbers I get only the first byte 
 and first half of the second byte - for instance: 'x06\x33' 
 comes out as 'x063'. This is very confusing 

Look closely and you will see that Python is telling you 
that the 06 represents the hex value of the byte. The second 
byte has no \x so is a character. The ASCII value of 0x33 
is '3'...

 hex(ord('3'))
'0x33'


 ...but 06 52 becomes 065. Any ideas?

Not on that one... x52 is 'R'... and chr(52) is '4'

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb ?

2005-11-17 Thread Ed Singleton
These both work (though neither is very graceful).

text = hello
message = 

for i in range(len(text)):
   message = message + text[(len(text)-i-1)]

print message


lst = list(text)
newstr = 

for item in text:
   newstr += (lst.pop())

print newstr

On 16/11/05, Chad Everett [EMAIL PROTECTED] wrote:
 Hello all,

 Have a problem here with a challenge from a book I am reading.
 Any help is much appreciated.

 I am trying to run a program that asks the user for a statement and then
 prints it out backwards.
 this is what I have.
 It does not print anything out.  I assume that I have something out of whack
 with my high and low statements.

 Thanks for you help.


 print \n\nWelcome to the Backwards Message Display.
 print
 message = raw_input(\nPlease Enter a Message.)

 high = len(message)
 low = -len(message)
 print
 print message[high:low]
 print
 print raw_input(Please Press Enter to Exit)


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

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb ?

2005-11-17 Thread Liam Clarke-Hutchinson
How about -
print \n\nWelcome to the Backwards Message Display.
print
message = raw_input(\nPlease Enter a Message.)
msgAsList = [ char for char in message]
msgAsList.reverse()
reversedMessage = ''.join(msgAsList)

I can't test that, but it should work.

But, with regard to - 

 print \n\nWelcome to the Backwards Message Display.
 print
 message = raw_input(\nPlease Enter a Message.)

 high = len(message)
 low = -len(message)
 print
 print message[high:low]
 print
 print raw_input(Please Press Enter to Exit)

low will always be the first char of the string, won't it? In which case,
It'd always be 0. or len(message) - len(message) !

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Ed Singleton
Sent: Friday, 18 November 2005 6:11 a.m.
To: tutor@python.org
Subject: Re: [Tutor] Newb ?


These both work (though neither is very graceful).

text = hello
message = 

for i in range(len(text)):
   message = message + text[(len(text)-i-1)]

print message


lst = list(text)
newstr = 

for item in text:
   newstr += (lst.pop())

print newstr

On 16/11/05, Chad Everett [EMAIL PROTECTED] wrote:
 Hello all,

 Have a problem here with a challenge from a book I am reading. Any 
 help is much appreciated.

 I am trying to run a program that asks the user for a statement and 
 then prints it out backwards. this is what I have.
 It does not print anything out.  I assume that I have something out of
whack
 with my high and low statements.

 Thanks for you help.


 print \n\nWelcome to the Backwards Message Display.
 print
 message = raw_input(\nPlease Enter a Message.)

 high = len(message)
 low = -len(message)
 print
 print message[high:low]
 print
 print raw_input(Please Press Enter to Exit)


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

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

A new monthly electronic newsletter covering all aspects of MED's work is now 
available.  Subscribers can choose to receive news from any or all of seven 
categories, free of charge: Growth and Innovation, Strategic Directions, Energy 
and Resources, Business News, ICT, Consumer Issues and Tourism.  See 
http://news.business.govt.nz for more details.




http://www.govt.nz - connecting you to New Zealand central  local government 
services

Any opinions expressed in this message are not necessarily those of the 
Ministry of Economic Development. This message and any files transmitted with 
it are confidential and solely for the use of the intended recipient. If you 
are not the intended recipient or the person responsible for delivery to the 
intended recipient, be advised that you have received this message in error and 
that any use is strictly prohibited. Please contact the sender and delete the 
message and any attachment from your computer.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb ?

2005-11-17 Thread Christian Wyglendowski
Liam said:
 
 How about -
 print \n\nWelcome to the Backwards Message Display.
 print
 message = raw_input(\nPlease Enter a Message.)
 msgAsList = [ char for char in message]

You could also do:

msgAsList = list(message)

list() takes any iterable and returns a list object.

 msgAsList.reverse()
 reversedMessage = ''.join(msgAsList)

In Python 2.4, the following is also possible:

reversedMessage = ''.join(reversed(list(message)))

It's amazing how in Python even one-liners can be so pretty :-)

Christian
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb ?

2005-11-17 Thread Liam Clarke-Hutchinson

list() takes any iterable and returns a list object.
Hmm, I did not know that the list(str) thing worked that way.That'll reduce
the list comprehensions, but is it intuitive? Would a newbie see that and
think that list(Hi) returns [Hi] or [H,i] ?
 
reversedMessage = ''.join(reversed(list(message)))

Yes, but for clarity when demonstrating a concept, one-liners should be
minimised, I think. ;)
But I agree, you can create some obscenely complicated one liners, which are
still understandable.
I find I have to force myself sometimes to refactor some of the ones I come
up with, however. Good rule of thumb is when you're feeling impressed with
your own cleverness, you probably need to refactor. 
(Thanks c2 wiki, for teaching me that.)


-Original Message-
From: Christian Wyglendowski [mailto:[EMAIL PROTECTED] 
Sent: Friday, 18 November 2005 9:31 a.m.
To: Liam Clarke-Hutchinson; tutor@python.org
Subject: RE: [Tutor] Newb ?


Liam said:
 
 How about -
 print \n\nWelcome to the Backwards Message Display.
 print
 message = raw_input(\nPlease Enter a Message.)
 msgAsList = [ char for char in message]

You could also do:

msgAsList = list(message)

list() takes any iterable and returns a list object.

 msgAsList.reverse()
 reversedMessage = ''.join(msgAsList)

In Python 2.4, the following is also possible:

reversedMessage = ''.join(reversed(list(message)))

It's amazing how in Python even one-liners can be so pretty :-)

Christian

A new monthly electronic newsletter covering all aspects of MED's work is now 
available.  Subscribers can choose to receive news from any or all of seven 
categories, free of charge: Growth and Innovation, Strategic Directions, Energy 
and Resources, Business News, ICT, Consumer Issues and Tourism.  See 
http://news.business.govt.nz for more details.




http://www.govt.nz - connecting you to New Zealand central  local government 
services

Any opinions expressed in this message are not necessarily those of the 
Ministry of Economic Development. This message and any files transmitted with 
it are confidential and solely for the use of the intended recipient. If you 
are not the intended recipient or the person responsible for delivery to the 
intended recipient, be advised that you have received this message in error and 
that any use is strictly prohibited. Please contact the sender and delete the 
message and any attachment from your computer.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newb ?

2005-11-17 Thread Christian Wyglendowski
Orri said:
 Or you could just do the following:
 
  print \n\nWelcome to the Backwards Message Display.
  print
  message = raw_input(\nPlease Enter a Message.)
  print message[::-1]

Interesting.  I forgot about the 'step' option when slicing.
 
 This is the equivalent of print ''.join(reversed(message)), since 
 reversed works on any iterable sequence, including strings.  

Ha!  Good call.  Makes it even clearer.

 In any 
 case, the syntax for this sort of thing in general is: 
 sequence[start:stop:step], with start defaulting to 0, step 
 defaulting 
 to sys.maxint (which, for all intents and purposes, means the 
 end of the 
 string), and step defaulting to 1.  However, when step is negative, 
 start and end switch defaults.  So by doing [::-1], you're telling 
 Python to return the values of the sequence that can be found 
 from the 
 end to the start

Cool.  Thanks for this explanation.

Christian
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb problem running the translator (fwd)

2005-04-09 Thread Max Noel
On Apr 9, 2005, at 10:36, Danny Yoo wrote:

-- Forwarded message --
Date: Fri, 8 Apr 2005 22:03:58 EDT
From: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Subject: newb problem running the translator
I will try to make this quick. I am a newb to python, and 
programming at
that, but i am realy interested. I have been looking at simple 
programs to be
run through the shell but i cannot figure out the translator. When i 
save
program i have tried to tell it to opoen it in the translator. I have 
been looking
through several websites and looked through the documents my python
installation came with. On one computer i am running Windows Xp and on 
another win98SE.
If you could explain to me how to open a program through the 
translator i
would appreciate it alot.
	What exactly do you call the translator?
	If you mean the compiler, it's normal that you can't find it: there is 
none. Python is an interpreted language (well, bytecode, but let's not 
get into that, shall we?). To run your Python programs, you call the 
Python interpreter on them: python foo.py, for example, runs the 
foo.py program.

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor