Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-18 Thread thebjorn
On Feb 15, 8:55 pm, Jeff Schwab [EMAIL PROTECTED] wrote:
 W. Watson wrote:
  See Subject. It's a simple txt file, each line is a Python stmt, but I
  need up to four digits added to each line with a space between the
  number field and the text. Perhaps someone has already done this or
  there's a source on the web for it. I'm not yet into files with Python.
  A sudden need has burst upon me. I'm using Win XP.

 i = 0
 for line in sys.stdin:
  i += 1
  print i, line,

 Or if you want consistent alignment:

 i = 0
 for line in sys.stdin:
  i += 1
  print %4s % i, line,

I like your version best (it's very clean and easy to understand), but
here's a few more versions...

  from itertools import count

  for i, line in zip(count(1), open('filename.txt')):
  print i, line,

or with consistent alignment:

  for x in zip(count(1), open('filename.txt')):
  print %4d %s % x,

the latter version gives rise to a one-liner

  open('output.txt','w').writelines('%4d %s' % x for x in
zip(count(1), open('perms.py')))

but as I said, I like the simple for loop the best ;-)

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


Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-18 Thread Michael Wronna


On Thu, 14 Feb 2008, W. Watson wrote:

 See Subject. It's a simple txt file, each line is a Python stmt, but I need 
 up to four digits added to each line with a space between the number field 
 and the text. Perhaps someone has already done this or there's a source on 
 the web for it. I'm not yet into files with Python. A sudden need has burst 
 upon me. I'm using Win XP.
 --
 Wayne Watson (Nevada City, CA)

   Web Page: speckledwithStars.net


Hi Wayne, sorry for that: Change OS, and type cat -n program.py  
numbered.py
Just joking, Mike
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-18 Thread William Pursell
On Feb 14, 6:54 am, W. Watson [EMAIL PROTECTED] wrote:
 See Subject. It's a simple txt file, each line is a Python stmt, but I need
 up to four digits added to each line with a space between the number field
 and the text. Perhaps someone has already done this or there's a source on
 the web for it. I'm not yet into files with Python. A sudden need has burst
 upon me. I'm using Win XP.

Not sure if Python program/tool means a tool or a program
in Python, but if awk is okay, that's the tool I would use:

awk '{printf( %4d %s\n, NR % 1, $0 )}'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-17 Thread JussiJ
On Feb 14, 5:54 pm, W. Watson [EMAIL PROTECTED] wrote:

 See Subject. It's a simple txt file, each line is a Python stmt,
 but I need up to four digits added to each line with a space
 between the number field and the text.

FWIW here is a Zeus editor, Python macro script to do this:

import zeus

def key_macro():
zeus.screen_update_disable()

# get the line is the current document
line_total = zeus.get_line_count()

# message in status bar
zeus.message(Line Count: %d % (line_total))

# write the line number to the start of each line
for i in range(1, line_total + 1):
zeus.set_line_pos(i, 1)
number = %4d  % (i)
zeus.write(number)

zeus.screen_update_enable()
zeus.screen_update()

key_macro() # run the macro

To use the script, just load the text file into Zeus and then
run the macro above from within Zeus.

Jussi Jumppanen
Author: Zeus for Windows IDE
http://www.zeusedit.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-15 Thread Steve Holden
W. Watson wrote:
 Thanks. I found this to work:
 
 input_file=open('junkin.txt','r')
 output_file=open('junkout.txt','w')
 for (line_cnt, each_line) in enumerate(input_file):
  output_file.write('%4d '%(line_cnt+1)+each_line)

You can improve this slightly by using string formatting more fully:

 output_file.write(%4d %s % (line_ct+1, each_line))

 output_file.close()
 input_file.close()
 
regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-15 Thread [EMAIL PROTECTED]
 See Subject. It's a simple txt file, each line is a Python stmt, but I need
 up to four digits added to each line with a space between the number field
 and the text. Perhaps someone has already done this or there's a source on
 the web for it. I'm not yet into files with Python. A sudden need has burst
 upon me. I'm using Win XP.

I'm not sure why you need the line numbers inserted into the file.
Alot of
editors will display and print line numbers.

My Visual Studio IDE displays file line numbers (it's an option) and
prints
them.  I believe there's a free Express Edition that you can download.

Also, check out http://www.textpad.com/ .  I think you can download an
evaluation copy.

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


Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-15 Thread Steve Holden
[EMAIL PROTECTED] wrote:
 See Subject. It's a simple txt file, each line is a Python stmt, but I need
 up to four digits added to each line with a space between the number field
 and the text. Perhaps someone has already done this or there's a source on
 the web for it. I'm not yet into files with Python. A sudden need has burst
 upon me. I'm using Win XP.
 
 I'm not sure why you need the line numbers inserted into the file.
 Alot of
 editors will display and print line numbers.
 
Perhaps so, but I had to insert line numbers into the code examples in 
Python Web Programming, for example, because I annotated the code 
using the line numbers and I wasn't about to allow the publishers the 
chance to screw them up.

 My Visual Studio IDE displays file line numbers (it's an option) and
 prints
 them.  I believe there's a free Express Edition that you can download.
 
 Also, check out http://www.textpad.com/ .  I think you can download an
 evaluation copy.
 
None of which would have answered by use case. Besides which, maybe the 
OP was just writing a test piece ...

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-15 Thread Jeff Schwab
W. Watson wrote:
 See Subject. It's a simple txt file, each line is a Python stmt, but I 
 need up to four digits added to each line with a space between the 
 number field and the text. Perhaps someone has already done this or 
 there's a source on the web for it. I'm not yet into files with Python. 
 A sudden need has burst upon me. I'm using Win XP.

i = 0
for line in sys.stdin:
 i += 1
 print i, line,

Or if you want consistent alignment:

i = 0
for line in sys.stdin:
 i += 1
 print %4s % i, line,


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


Re: [OT] Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-15 Thread Steve Holden
I'll see your IBM 650 and raise you a Univac 418 with a FastRand drum.

regards
  Steve

W. Watson wrote:
 Good grief! You go a long way back. Want to try for an IBM 650 with a drum 
 memory?
 
 Gabriel Genellina wrote:
 En Thu, 14 Feb 2008 04:54:56 -0200, W. Watson [EMAIL PROTECTED] 
 escribió:

 See Subject. It's a simple txt file, each line is a Python stmt, but I 
 need
 up to four digits added to each line with a space between the number 
 field
 and the text. Perhaps someone has already done this or there's a 
 source on
 the web for it. I'm not yet into files with Python. A sudden need has 
 burst
 upon me. I'm using Win XP.
 This command should suffice - but you must first find a working CP/M 
 system to use it:

 CPIP [N] NEW.PY=OLD.PY

 (Sorry - just a a nostalgic flash!)

 


-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-14 Thread John Machin
On Feb 14, 6:13 pm, Chris [EMAIL PROTECTED] wrote:
 On Feb 14, 8:54 am, W. Watson [EMAIL PROTECTED] wrote:

  See Subject. It's a simple txt file, each line is a Python stmt, but I need
  up to four digits added to each line with a space between the number field
  and the text. Perhaps someone has already done this or there's a source on
  the web for it. I'm not yet into files with Python. A sudden need has burst
  upon me. I'm using Win XP.
  --
Wayne Watson (Nevada City, CA)

  Web Page: speckledwithStars.net

 enumerate through the file which will yield you a counter (starting @
 zero so just add 1) and use the string function .zfill() to pad it out
 for you.
 eg.

 for (line_cnt, each_line) in enumerate(input_file):
 output_file.write(print ('%s '%(line_cnt+1)).zfill(5) + each_line)

(1) What's that print doing in there?
(2) zfill(5)? The OP asked for up to 4 digits, not 5.
(2) As an alternative to str.zfill, consider using formatting:

output_file.write('%04d %s' % (line_cnt+1, each_line))

And what does up to 4 mean? What does the OP want the 1th line
to look like?

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


Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-14 Thread Chris
On Feb 14, 1:29 pm, John Machin [EMAIL PROTECTED] wrote:
 On Feb 14, 6:13 pm, Chris [EMAIL PROTECTED] wrote:



  On Feb 14, 8:54 am, W. Watson [EMAIL PROTECTED] wrote:

   See Subject. It's a simple txt file, each line is a Python stmt, but I 
   need
   up to four digits added to each line with a space between the number field
   and the text. Perhaps someone has already done this or there's a source on
   the web for it. I'm not yet into files with Python. A sudden need has 
   burst
   upon me. I'm using Win XP.
   --
 Wayne Watson (Nevada City, CA)

   Web Page: speckledwithStars.net

  enumerate through the file which will yield you a counter (starting @
  zero so just add 1) and use the string function .zfill() to pad it out
  for you.
  eg.

  for (line_cnt, each_line) in enumerate(input_file):
  output_file.write(print ('%s '%(line_cnt+1)).zfill(5) + each_line)

 (1) What's that print doing in there?
 (2) zfill(5)? The OP asked for up to 4 digits, not 5.
 (2) As an alternative to str.zfill, consider using formatting:

 output_file.write('%04d %s' % (line_cnt+1, each_line))

 And what does up to 4 mean? What does the OP want the 1th line
 to look like?

print was a typo
take a look at the string that is built before the zfill fires, it has
a trailing space so it is correct. ;)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-14 Thread W. Watson
Thanks. I found this to work:

input_file=open('junkin.txt','r')
output_file=open('junkout.txt','w')
for (line_cnt, each_line) in enumerate(input_file):
 output_file.write('%4d '%(line_cnt+1)+each_line)
output_file.close()
input_file.close()

I removed the print, but ran into trouble with zfill. I thought this might 
be more difficult judging by a long ago experience with Java.

Chris wrote:
 On Feb 14, 1:29 pm, John Machin [EMAIL PROTECTED] wrote:
 On Feb 14, 6:13 pm, Chris [EMAIL PROTECTED] wrote:



 On Feb 14, 8:54 am, W. Watson [EMAIL PROTECTED] wrote:
 See Subject. It's a simple txt file, each line is a Python stmt, but I need
 up to four digits added to each line with a space between the number field
 and the text. Perhaps someone has already done this or there's a source on
 the web for it. I'm not yet into files with Python. A sudden need has burst
 upon me. I'm using Win XP.
 --
   Wayne Watson (Nevada City, CA)
 Web Page: speckledwithStars.net
 enumerate through the file which will yield you a counter (starting @
 zero so just add 1) and use the string function .zfill() to pad it out
 for you.
 eg.
 for (line_cnt, each_line) in enumerate(input_file):
 output_file.write(print ('%s '%(line_cnt+1)).zfill(5) + each_line)
 (1) What's that print doing in there?
 (2) zfill(5)? The OP asked for up to 4 digits, not 5.
 (2) As an alternative to str.zfill, consider using formatting:

 output_file.write('%04d %s' % (line_cnt+1, each_line))

 And what does up to 4 mean? What does the OP want the 1th line
 to look like?
 
 print was a typo
 take a look at the string that is built before the zfill fires, it has
 a trailing space so it is correct. ;)

-- 
  Wayne Watson (Nevada City, CA)

Web Page: speckledwithStars.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-14 Thread J Peyret
On Feb 14, 8:50 am, W. Watson [EMAIL PROTECTED] wrote:

(snip)

 I thought this might be more difficult judging by a long ago experience with 
 Java.

(snip)

+1 QOTW
-- 
http://mail.python.org/mailman/listinfo/python-list


[OT] Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-14 Thread Gabriel Genellina
En Thu, 14 Feb 2008 04:54:56 -0200, W. Watson [EMAIL PROTECTED]  
escribió:

 See Subject. It's a simple txt file, each line is a Python stmt, but I  
 need
 up to four digits added to each line with a space between the number  
 field
 and the text. Perhaps someone has already done this or there's a source  
 on
 the web for it. I'm not yet into files with Python. A sudden need has  
 burst
 upon me. I'm using Win XP.

This command should suffice - but you must first find a working CP/M  
system to use it:

CPIP [N] NEW.PY=OLD.PY

(Sorry - just a a nostalgic flash!)

-- 
Gabriel Genellina

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


Re: [OT] Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-14 Thread W. Watson
Good grief! You go a long way back. Want to try for an IBM 650 with a drum 
memory?

Gabriel Genellina wrote:
 En Thu, 14 Feb 2008 04:54:56 -0200, W. Watson [EMAIL PROTECTED] 
 escribió:
 
 See Subject. It's a simple txt file, each line is a Python stmt, but I 
 need
 up to four digits added to each line with a space between the number 
 field
 and the text. Perhaps someone has already done this or there's a 
 source on
 the web for it. I'm not yet into files with Python. A sudden need has 
 burst
 upon me. I'm using Win XP.
 
 This command should suffice - but you must first find a working CP/M 
 system to use it:
 
 CPIP [N] NEW.PY=OLD.PY
 
 (Sorry - just a a nostalgic flash!)
 

-- 
  Wayne Watson (Nevada City, CA)

Web Page: speckledwithStars.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [OT] Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-14 Thread Jaap Spies
Gabriel Genellina wrote:
 En Thu, 14 Feb 2008 04:54:56 -0200, W. Watson [EMAIL PROTECTED] 
 escribió:
 
 See Subject. It's a simple txt file, each line is a Python stmt, but I 
 need
 up to four digits added to each line with a space between the number 
 field
 and the text. Perhaps someone has already done this or there's a 
 source on
 the web for it. I'm not yet into files with Python. A sudden need has 
 burst
 upon me. I'm using Win XP.
 
 This command should suffice - but you must first find a working CP/M 
 system to use it:
 
 CPIP [N] NEW.PY=OLD.PY
 
 (Sorry - just a a nostalgic flash!)
 

Wow! You remembered this. Good old PIP!

Jaap

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


Re: [OT] Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-14 Thread Gabriel Genellina
 Gabriel Genellina wrote:
 En Thu, 14 Feb 2008 04:54:56 -0200, W. Watson [EMAIL PROTECTED]
 escribió:

 See Subject. It's a simple txt file, each line is a Python stmt, but I
 need
 up to four digits added to each line with a space between the number
 field
 and the text.

 This command should suffice - but you must first find a working CP/M
 system to use it:

 CPIP [N] NEW.PY=OLD.PY

 (Sorry - just a a nostalgic flash!)

En Thu, 14 Feb 2008 19:47:16 -0200, Jaap Spies [EMAIL PROTECTED]  
escribió:

 Wow! You remembered this. Good old PIP!

So powerful... It was one of the first things I learned, perhaps that's  
why I still remember it.

En Thu, 14 Feb 2008 18:53:16 -0200, W. Watson [EMAIL PROTECTED]  
escribió:

 Good grief! You go a long way back. Want to try for an IBM 650 with a  
 drum
 memory?

I can't go s long back in time :) but I remember having used a  
Winchester removable hard drive, maybe 30MB capacity, that made a terrible  
noise and had to be powered on a few minutes earlier than the main unit  
because it had to speed up.

-- 
Gabriel Genellina

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


Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-13 Thread W. Watson
See Subject. It's a simple txt file, each line is a Python stmt, but I need 
up to four digits added to each line with a space between the number field 
and the text. Perhaps someone has already done this or there's a source on 
the web for it. I'm not yet into files with Python. A sudden need has burst 
upon me. I'm using Win XP.
-- 
  Wayne Watson (Nevada City, CA)

Web Page: speckledwithStars.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File

2008-02-13 Thread Chris
On Feb 14, 8:54 am, W. Watson [EMAIL PROTECTED] wrote:
 See Subject. It's a simple txt file, each line is a Python stmt, but I need
 up to four digits added to each line with a space between the number field
 and the text. Perhaps someone has already done this or there's a source on
 the web for it. I'm not yet into files with Python. A sudden need has burst
 upon me. I'm using Win XP.
 --
   Wayne Watson (Nevada City, CA)

 Web Page: speckledwithStars.net

enumerate through the file which will yield you a counter (starting @
zero so just add 1) and use the string function .zfill() to pad it out
for you.
eg.

for (line_cnt, each_line) in enumerate(input_file):
output_file.write(print ('%s '%(line_cnt+1)).zfill(5) + each_line)
output_file.close()
input_file.close()
-- 
http://mail.python.org/mailman/listinfo/python-list