470,596 Members | 1,455 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

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

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>
Feb 14 '08 #1
18 2280
On Feb 14, 8:54 am, "W. Watson" <wolf_tra...@invalid.comwrote:
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()
Feb 14 '08 #2
On Feb 14, 6:13 pm, Chris <cwi...@gmail.comwrote:
On Feb 14, 8:54 am, "W. Watson" <wolf_tra...@invalid.comwrote:
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 10000th line
to look like?

Feb 14 '08 #3
On Feb 14, 1:29 pm, John Machin <sjmac...@lexicon.netwrote:
On Feb 14, 6:13 pm, Chris <cwi...@gmail.comwrote:
On Feb 14, 8:54 am, "W. Watson" <wolf_tra...@invalid.comwrote:
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 10000th 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. ;)
Feb 14 '08 #4
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 <sjmac...@lexicon.netwrote:
>On Feb 14, 6:13 pm, Chris <cwi...@gmail.comwrote:
>>On Feb 14, 8:54 am, "W. Watson" <wolf_tra...@invalid.comwrote:
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 10000th 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>
Feb 14 '08 #5
On Feb 14, 8:50 am, "W. Watson" <wolf_tra...@invalid.comwrote:

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

+1 QOTW
Feb 14 '08 #6
En Thu, 14 Feb 2008 04:54:56 -0200, W. Watson <wo*********@invalid.com>
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:

C>PIP [N] NEW.PY=OLD.PY

(Sorry - just a a nostalgic flash!)

--
Gabriel Genellina

Feb 14 '08 #7
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 <wo*********@invalid.com>
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:

C>PIP [N] NEW.PY=OLD.PY

(Sorry - just a a nostalgic flash!)
--
Wayne Watson (Nevada City, CA)

Web Page: <speckledwithStars.net>
Feb 14 '08 #8
Gabriel Genellina wrote:
En Thu, 14 Feb 2008 04:54:56 -0200, W. Watson <wo*********@invalid.com>
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:

C>PIP [N] NEW.PY=OLD.PY

(Sorry - just a a nostalgic flash!)
Wow! You remembered this. Good old PIP!

Jaap

Feb 14 '08 #9
Gabriel Genellina wrote:
>En Thu, 14 Feb 2008 04:54:56 -0200, W. Watson <wo*********@invalid.com>
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:

C>PIP [N] NEW.PY=OLD.PY

(Sorry - just a a nostalgic flash!)
En Thu, 14 Feb 2008 19:47:16 -0200, Jaap Spies <j.*****@hccnet.nl>
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 <wo*********@invalid.com>
escribió:
Good grief! You go a long way back. Want to try for an IBM 650 with a
drum
memory?
I can't go soooo 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

Feb 14 '08 #10
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/

Feb 15 '08 #11
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
Feb 15 '08 #12
An**********@gmail.com 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/

Feb 15 '08 #13
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 <wo*********@invalid.com>
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:

C>PIP [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/

Feb 15 '08 #14
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,
Feb 15 '08 #15
On Feb 15, 8:55 pm, Jeff Schwab <j...@schwabcenter.comwrote:
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
Feb 18 '08 #16


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
Feb 18 '08 #17
On Feb 14, 6:54 am, "W. Watson" <wolf_tra...@invalid.comwrote:
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 % 10000, $0 )}'
Feb 18 '08 #18
-----Original Message-----
From: py********************************@python.org [mailto:python-
li*************************@python.org] On Behalf Of William Pursell
Sent: Monday, February 18, 2008 8:37 AM
To: py*********@python.org
Subject: Re: Looking for a Python Program/Tool That Will Add Line
Numbers to atxt File

On Feb 14, 6:54 am, "W. Watson" <wolf_tra...@invalid.comwrote:
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 % 10000, $0 )}'

On a related note, since it's probably easier to install Perl instead of
awk on a windows box:

type foo.java | perl -ne "$counter++; print sprintf(qq(%4d ), $counter),
$_;"
Feb 18 '08 #19

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

699 posts views Thread by mike420 | last post: by
226 posts views Thread by Stephen C. Waterbury | last post: by
52 posts views Thread by Olivier Scalbert | last post: by
37 posts views Thread by Carlos Ribeiro | last post: by
10 posts views Thread by Paul Kooistra | last post: by
68 posts views Thread by Lad | last post: by
56 posts views Thread by Omar | last post: by
Banfa
2 posts views Thread by Banfa | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.