473,698 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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: <speckledwithSt ars.net>
Feb 14 '08 #1
18 2433
On Feb 14, 8:54 am, "W. Watson" <wolf_tra...@in valid.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: <speckledwithSt ars.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.wri te(print ('%s '%(line_cnt+1)) .zfill(5) + each_line)
output_file.clo se()
input_file.clos e()
Feb 14 '08 #2
On Feb 14, 6:13 pm, Chris <cwi...@gmail.c omwrote:
On Feb 14, 8:54 am, "W. Watson" <wolf_tra...@in valid.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: <speckledwithSt ars.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.wri te(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.wri te('%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...@lexic on.netwrote:
On Feb 14, 6:13 pm, Chris <cwi...@gmail.c omwrote:
On Feb 14, 8:54 am, "W. Watson" <wolf_tra...@in valid.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: <speckledwithSt ars.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.wri te(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.wri te('%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=ope n('junkout.txt' ,'w')
for (line_cnt, each_line) in enumerate(input _file):
output_file.wri te('%4d '%(line_cnt+1)+ each_line)
output_file.clo se()
input_file.clos e()

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...@lexic on.netwrote:
>On Feb 14, 6:13 pm, Chris <cwi...@gmail.c omwrote:
>>On Feb 14, 8:54 am, "W. Watson" <wolf_tra...@in valid.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: <speckledwithSt ars.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.wri te(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.wr ite('%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: <speckledwithSt ars.net>
Feb 14 '08 #5
On Feb 14, 8:50 am, "W. Watson" <wolf_tra...@in valid.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*********@in valid.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*********@in valid.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: <speckledwithSt ars.net>
Feb 14 '08 #8
Gabriel Genellina wrote:
En Thu, 14 Feb 2008 04:54:56 -0200, W. Watson <wo*********@in valid.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*********@in valid.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*********@in valid.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
33932
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
226
12584
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 "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
49
2843
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 Vainio http://www.students.tut.fi/~vainio24
52
3754
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
2615
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 activities -- doing development for Python in the Windows environment -- are not being succesful as I had originally dreamt. I have big constraints on what can I do now; money is not an option, and my current machine is still useful but it's below...
10
2027
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. Each line (record) contains an 2-character identifier, like 'A0' or 'C1'. The identifier identifies the record format for the line, thereby allowing different record formats to be used in a textfile. For example: An A0 record may consist of:
68
5856
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
56
3250
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
19342
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 we set about doing that. Every program starts with a specification, this may be a several hundred page document from your latest client or one small paragraph from your professor and pretty much anything in-between. The specification is very...
0
9156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9021
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7716
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.