473,383 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 2408
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. But, Python does not a have very good macro...
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 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
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, mentioning that Ruby is more "refined". -- Ville...
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 frustrated. My search for a good IDE to support my...
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 to create printed pages by an external company. ...
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 run programs right from within d) can edit
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 constituting a valid program. Now we will discuss how...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.