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

W

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.
 
C

Chris

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.

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()
 
J

John Machin

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?
 
C

Chris

(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. ;)
 
W

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.
 
G

Gabriel Genellina

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!)
 
W

W. Watson

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

Gabriel said:
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!)
 
J

Jaap Spies

Gabriel said:
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
 
G

Gabriel Genellina

Gabriel said:
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!)

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.

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".
 
S

Steve Holden

W. Watson said:
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
 
A

AnonMail2005

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
 
S

Steve Holden

I'll see your IBM 650 and raise you a Univac 418 with a FastRand drum.

regards
Steve

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

Gabriel said:
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!)
 
S

Steve Holden

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
 
J

Jeff Schwab

W. Watson said:
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,
 
J

JussiJ

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
 
T

thebjorn

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
 
M

Michael Wronna

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.

Hi Wayne, sorry for that: Change OS, and type cat -n program.py >
numbered.py
Just joking, Mike
 
W

William Pursell

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 )}'
 
R

Reedick, Andrew

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of William Pursell
Sent: Monday, February 18, 2008 8:37 AM
To: (e-mail address removed)
Subject: Re: 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.

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),
$_;"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,219
Latest member
KristieKoh

Latest Threads

Top