Indexing lines of a txt file [duplicate]












0
















This question already has an answer here:




  • How to add line numbers to an output file?

    2 answers



  • PYTHON- adding line numbers to the output

    3 answers




I'm trying to find a way to index the lines of a text file.



I use the following code to read the txt data



with open('file.txt','r') as f:
lines=f.read()
print(lines)`


the result is



Aberdeen, Scotland
Adelaide, Australia
Algiers, Algeria


but I would like to put 1:,2:,3: in front of each line.So I suppose that I have to do something like the following



with open('file.txt','a') as  f:
for i in len(f):
line='i:'+(ith line of txt file)
f.write(line)


but I have a hard time solving it, any help will be great.










share|improve this question















marked as duplicate by Anto Jurković, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 16:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Im going to check it out

    – G1I2A
    Nov 27 '18 at 14:43
















0
















This question already has an answer here:




  • How to add line numbers to an output file?

    2 answers



  • PYTHON- adding line numbers to the output

    3 answers




I'm trying to find a way to index the lines of a text file.



I use the following code to read the txt data



with open('file.txt','r') as f:
lines=f.read()
print(lines)`


the result is



Aberdeen, Scotland
Adelaide, Australia
Algiers, Algeria


but I would like to put 1:,2:,3: in front of each line.So I suppose that I have to do something like the following



with open('file.txt','a') as  f:
for i in len(f):
line='i:'+(ith line of txt file)
f.write(line)


but I have a hard time solving it, any help will be great.










share|improve this question















marked as duplicate by Anto Jurković, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 16:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Im going to check it out

    – G1I2A
    Nov 27 '18 at 14:43














0












0








0









This question already has an answer here:




  • How to add line numbers to an output file?

    2 answers



  • PYTHON- adding line numbers to the output

    3 answers




I'm trying to find a way to index the lines of a text file.



I use the following code to read the txt data



with open('file.txt','r') as f:
lines=f.read()
print(lines)`


the result is



Aberdeen, Scotland
Adelaide, Australia
Algiers, Algeria


but I would like to put 1:,2:,3: in front of each line.So I suppose that I have to do something like the following



with open('file.txt','a') as  f:
for i in len(f):
line='i:'+(ith line of txt file)
f.write(line)


but I have a hard time solving it, any help will be great.










share|improve this question

















This question already has an answer here:




  • How to add line numbers to an output file?

    2 answers



  • PYTHON- adding line numbers to the output

    3 answers




I'm trying to find a way to index the lines of a text file.



I use the following code to read the txt data



with open('file.txt','r') as f:
lines=f.read()
print(lines)`


the result is



Aberdeen, Scotland
Adelaide, Australia
Algiers, Algeria


but I would like to put 1:,2:,3: in front of each line.So I suppose that I have to do something like the following



with open('file.txt','a') as  f:
for i in len(f):
line='i:'+(ith line of txt file)
f.write(line)


but I have a hard time solving it, any help will be great.





This question already has an answer here:




  • How to add line numbers to an output file?

    2 answers



  • PYTHON- adding line numbers to the output

    3 answers








python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 14:38









TrebuchetMS

2,85511023




2,85511023










asked Nov 27 '18 at 14:33









G1I2AG1I2A

62




62




marked as duplicate by Anto Jurković, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 16:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Anto Jurković, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 16:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Im going to check it out

    – G1I2A
    Nov 27 '18 at 14:43



















  • Im going to check it out

    – G1I2A
    Nov 27 '18 at 14:43

















Im going to check it out

– G1I2A
Nov 27 '18 at 14:43





Im going to check it out

– G1I2A
Nov 27 '18 at 14:43












2 Answers
2






active

oldest

votes


















0














You must handle two files, one for the input and one for the output:



with open("file.txt") as finp, open("newfile.txt","w") as fout:
for index,line in enumerate(finp):
fout.write(str(index)+": "+line)


If you want to start numbering from 1, write: enumerate(finp,1)






share|improve this answer

































    0














    You don't need two handle files. Just use seek.



    file_path = "file.txt"

    with open(file_path,"r+") as file:
    lines = file.readlines()
    file.seek(0)
    for index,line in enumerate(lines):
    to_write_string = str(index)+': '+line
    file.write(to_write_string)


    This should do the trick..






    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You must handle two files, one for the input and one for the output:



      with open("file.txt") as finp, open("newfile.txt","w") as fout:
      for index,line in enumerate(finp):
      fout.write(str(index)+": "+line)


      If you want to start numbering from 1, write: enumerate(finp,1)






      share|improve this answer






























        0














        You must handle two files, one for the input and one for the output:



        with open("file.txt") as finp, open("newfile.txt","w") as fout:
        for index,line in enumerate(finp):
        fout.write(str(index)+": "+line)


        If you want to start numbering from 1, write: enumerate(finp,1)






        share|improve this answer




























          0












          0








          0







          You must handle two files, one for the input and one for the output:



          with open("file.txt") as finp, open("newfile.txt","w") as fout:
          for index,line in enumerate(finp):
          fout.write(str(index)+": "+line)


          If you want to start numbering from 1, write: enumerate(finp,1)






          share|improve this answer















          You must handle two files, one for the input and one for the output:



          with open("file.txt") as finp, open("newfile.txt","w") as fout:
          for index,line in enumerate(finp):
          fout.write(str(index)+": "+line)


          If you want to start numbering from 1, write: enumerate(finp,1)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 27 '18 at 16:06

























          answered Nov 27 '18 at 16:01









          kantalkantal

          642127




          642127

























              0














              You don't need two handle files. Just use seek.



              file_path = "file.txt"

              with open(file_path,"r+") as file:
              lines = file.readlines()
              file.seek(0)
              for index,line in enumerate(lines):
              to_write_string = str(index)+': '+line
              file.write(to_write_string)


              This should do the trick..






              share|improve this answer




























                0














                You don't need two handle files. Just use seek.



                file_path = "file.txt"

                with open(file_path,"r+") as file:
                lines = file.readlines()
                file.seek(0)
                for index,line in enumerate(lines):
                to_write_string = str(index)+': '+line
                file.write(to_write_string)


                This should do the trick..






                share|improve this answer


























                  0












                  0








                  0







                  You don't need two handle files. Just use seek.



                  file_path = "file.txt"

                  with open(file_path,"r+") as file:
                  lines = file.readlines()
                  file.seek(0)
                  for index,line in enumerate(lines):
                  to_write_string = str(index)+': '+line
                  file.write(to_write_string)


                  This should do the trick..






                  share|improve this answer













                  You don't need two handle files. Just use seek.



                  file_path = "file.txt"

                  with open(file_path,"r+") as file:
                  lines = file.readlines()
                  file.seek(0)
                  for index,line in enumerate(lines):
                  to_write_string = str(index)+': '+line
                  file.write(to_write_string)


                  This should do the trick..







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 27 '18 at 16:14









                  Richard RobertsRichard Roberts

                  84




                  84















                      Popular posts from this blog

                      Contact image not getting when fetch all contact list from iPhone by CNContact

                      count number of partitions of a set with n elements into k subsets

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks