But i dont know the line number before
I need to check for the file for some data.
for example i have a text file "hallo.txt"
######
Hallo.txt
######
1 11 21
2 12 22
3 13 23
4 14 24
5 15 25
6 16 26
7 17 27
8 18 28
Now i need to insert a line 2002 12 22 in the third line and my new file should look like this
1 11 21
2 12 22
2002 12 22
3 13 23
4 14 24
5 15 25
6 16 26
7 17 27
8 18 28
sometimes i need to insert 3003 13 33 after 3rd line and so on........
so i need to check each and every line and then if i find a match then i need to insert a line next to it
how can i find the desired line number from the file and then i need to insert some data to the file and then do the read operation.
Thanks
Freedom
Main Topics
Browse All Topics





by: efnPosted on 2005-05-14 at 00:50:05ID: 14001536
1. The line
singleLine = line.rstrip()
removes the newline character at the end of each input line. If you want to keep those characters, don't call that function.
2. The program does not simultaneously read and write. It reads the whole file in at once (input.readlines()), then writes it out one line at a time (out.write(singleLine)). So if you want to insert some data, you can write out some of the input lines, write out some inserted lines, then write out the rest of the input lines.
For example, to insert a line between the fifth and sixth lines of the input:
for line in s[:5]
out.write(line)
out.write("inserted line\n")
for line in s[5:]
out.write(line)