Indexing lines of a txt file [duplicate]
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.
python python-3.x
marked as duplicate by Anto Jurković, jpp
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.
add a comment |
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.
python python-3.x
marked as duplicate by Anto Jurković, jpp
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
add a comment |
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.
python python-3.x
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
python python-3.x
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
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
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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)
add a comment |
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..
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
add a comment |
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)
add a comment |
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)
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)
edited Nov 27 '18 at 16:06
answered Nov 27 '18 at 16:01
kantalkantal
642127
642127
add a comment |
add a comment |
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..
add a comment |
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..
add a comment |
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..
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..
answered Nov 27 '18 at 16:14
Richard RobertsRichard Roberts
84
84
add a comment |
add a comment |
Im going to check it out
– G1I2A
Nov 27 '18 at 14:43