472,328 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 2337
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn....
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python...
49
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though,...
52
by: Olivier Scalbert | last post by:
Hello , What is the python way of doing this : perl -pi -e 's/string1/string2/' file ? Thanks Olivier
37
by: Carlos Ribeiro | last post by:
Oh well. A mailing list is not the most appropriate place for rants (a blog is better), but it's still better than keeping it for myself. I'm...
10
by: Paul Kooistra | last post by:
I need a tool to browse text files with a size of 10-20 Mb. These files have a fixed record length of 800 bytes (CR/LF), and containt records used...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
56
by: Omar | last post by:
I'd love the perfect editor that would be: a) free b) enable me to drag and drop code snippets from a sort of browser into the code c) can...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.