How to write into nth line in a file with python - python

I wanted to write a line at particular line number, my file contains following data
one
two
three
four
five
Six
Seven
Eight
Nine
Ten
Eleven
Here is my code -
f = open("file.txt",'r+')
for i, line in enumerate(f):
if i == 2:
f.write("Added at 2nd line")
elif i == 3:
f.write("Added at 2nd line")
elif i > 29:
break
f.close()
after running above code i am getting O/P as below
one
two
three
four
five
Six
Seven
Eight
Nine
Ten
ElevenAdded at 2nd line
Please help me how to write to particular;ar number in file

Ihave tried it and it actually works, and really quickly
Go through the concept..
linecache
and if not this..then this is what u need to do:
Files in Python are iterators, meaning they can be looped over, or have iteration operations applied to them.
To get every 5th line, for example, would be:
import itertools
with open(filename, 'r') as f:
fifthlines = itertools.islice(f, 0, None, 5)
for line in fifthlines:
# do something with line
To skip a series of lines, use a noop for loop; here we skip 10 lines, then read 10:
for _ in itertools.islice(f, 0, 10):
pass
for line in itertools.islice(f, 0, 10):
# do something with this 10th line
With the itertools library, and a quick scan through the Python tutorial you can figure out the rest of the script easily enough.

Your question is not very clear to me, but this is the way i understand it. I tried to use your approach with enumerate and new variable line_num (your line number) which you can set manually in the script or from the terminal:
main_file = open('file.txt', 'r')
lines = []
line_num = 4 # if you want set it manually from script
#line_num = input('Type the line number: ') # if you want to set it from terminal
for i, line in enumerate(main_file):
if i+1 != line_num:
str_line = line
else:
# enumerate starts from zero so increment i by 1
str_line = line.rstrip('\n') + ' (added at %s line)\n' % (i+1)
lines.append(str_line)
if i > 29:
break
main_file.close()
with open('file.txt', 'w') as main_file:
main_file.writelines(lines)
I've divided work with files into two blocks:
- first is reading file lines and adds it to lines list (with added edited line)
- and second block is writing lines into files.txt writelines method concluded in with statement.

Never try to write in the same file you are reading unless it is a direct (binary) file with block of constant size ! You could break it and hardly recover.
In your use case it is not too worse. As you use the iterator interface of the file object, Python reads a whole chunk (in fact the whole file) and the file pointer is immediately positionned at the end. The you get the line one at a time, but when you write you (hopefully) write at the end of file. If you had read the file with readline, your write would have overwritten following lines.
The correct way is to rename input file, open it readonly, open a new file writeonly copy and modify at will and if was ok delete input file.
The alternative simple way when the file is small, is to load everything in memory and rewind the file before writing all back.
f = open("file.txt",'r+')
lines = f.readlines()
f.seek(0)
for i, line in enumerate(lines):
if i == 2:
f.write("Added at 2nd line")
elif i == 3:
f.write("Added at 2nd line")
elif i > 29:
break
f.close()

You can use fileinput:
import fileinput
for line in fileinput.input('file.txt', inplace=True):
if fileimput.filelineno() == 2:
print line.replace(line, 'Added at 2nd line\n'),
or you have to write a second file:
with open('file.txt', 'r') as input_file, open('new_file.txt', 'w') as output_file:
for line in input_file:
if input_file.index(line) == 1:
output_file.write('Added at 2nd line\n')
else:
output_file.write(line)

Related

How to get the last character in a file from Python?

I'm trying to set a variable to the last character of a file. I am using Python, and I'm fairly new to it. If it is of any importance, my code appends a random number between 2 and 9 to the end of an HTML file. In a separate function, I want to set the last character of the HTML file (the last character being the random number between 2 and 9) to a variable, then delete the last character (as to not affect the function of the HTML). Doe's anyone know how I could do this? I can attach my code below if needed, but I chose not to as it is 50 lines long and all 50 lines are needed for full context.
try this,
"a.txt" file has number 1, 3, 4, 5
Below code will read the file and pulls out last character from the file.
file = open('a.txt','r')
lines = file.read()
print(lines[-1])
=> 5
Using #Jab's answer from the comment above as well as some assumptions, we can produce a more efficient solution to finding the last character and replacing it.
The assumptions that are made are common and most likely will be valid:
You will know whether there is a newline character at the very end of the file, or whether the random number is truly the last character in the file (meaning accounting for whitespace).
You know the encoding of the file. This is valid since almost all HTML is utf-8, (can be utf-16), and since you are the one editing it, you will know. Most times the encoding won't even matter.
So, this is what we can do:
with open("test.txt", "rb+", encoding='utf-8') as f:
f.seek(-2, 2)
# -1 or -2, may change depending on whitespace characters at end of the file
var = f.read(1) # read one byte for a number
f.seek(-1,1)
print("last character:", str(var, 'utf-8'))
f.write(bytes('variable', 'utf-8')) # set whatever info here
f.write(bytes('\n', 'utf-8')) # you may want a newline character at the end of the file
f.truncate()
This is efficient because we actually don't have to iterate through the entire file. We iterate through just the last character, once to read and once to write.
You can do something like that:
# Open the file to read and the file to write
with open('file.txt'), open('new_file.txt', 'w+') as f_in, f_out:
# Read all the lines to memory (you can't find the last line lazily)
lines = f_in.readlines()
# Iterate over every line
for i, line in enumerate(lines):
# If the current index is the last index (i.e. the last line)
if i == len(lines) - 1:
# Get the last character
last_char = line[-1]
# Write to the output file the line without the last character
print(line[:-1], file=f_out, end='')
else:
# Write to the output file the line as it is
print(line, file=f_out, end='')
# Print the removed char
print(last_char)
If you don't want to create a new file, you can load all the file to memory as we're currently doing:
# Read all the lines into memory
with open('file.txt') as f:
lines = f.readlines()
# Replace the lines inside the list using the previous logic
for i, line in enumerate(lines):
if i == len(lines) - 1:
last_char = line[-1]
lines[i] = line[:-1]
else:
lines[i] = line
# Write the changed lines to the same file
with open('file.txt', 'w+') as f:
print(''.join(lines), file=f, end='')
# Print the removed char
print(last_char)

Read file and find if all lines are the same length

Using python I need to read a file and determine if all lines are the same length or not. If they are I move the file into a "good" folder and if they aren't all the same length I move them into a "bad" folder and write a word doc that says which line was not the same as the rest. Any help or ways to start?
You should use all():
with open(filename) as read_file:
length = len(read_file.readline())
if all(len(line) == length for line in read_file):
# Move to good folder
else:
# Move to bad folder
Since all() is short-circuiting, it will stop reading the file at the first non-match.
First off, you can read the file, here example.txt and put all lines in a list, content:
with open(filename) as f:
content = f.readlines()
Next you need to trim all the newline characters from the end of a line and put it in another list result:
for line in content:
line = line.strip()
result.append(line)
Now it's not that hard to get the length of every sentence, and since you want lines that are bad, you loop through the list:
for line in result:
lengths.append(len(line))
So the i-th element of result has length [i-th element of lengths]. We can make a counter for what line length occurs the most in the list, it is as simple as one line!
most_occuring = max(set(lengths), key=lengths.count)
Now we can make another for-loop to check which lengths don't correspond with the most-occuring and add those to bad-lines:
for i in range(len(lengths)):
if (lengths[i] != most_occuring):
bad_lines.append([i, result[i]])
The next step is check where the file needs to go, the good folder, or the bad folder:
if len(bad_lines == 0):
#Good file, move it to the good folder, use the os or shutil module
os.rename("path/to/current/file.foo", "path/to/new/desination/for/file.foo")
else:
#Bad file, one or more lines are bad, thus move it to the bad folder
os.rename("path/to/current/file.foo", "path/to/new/desination/for/file.foo")
The last step is writing the bad lines to another file, which is do-able, since we have the bad lines already in a list bad_lines:
with open("bad_lines.txt", "wb") as f:
for bad_line in bad_lines:
f.write("[%3i] %s\n" % (bad_line[0], bad_line[1]))
It's not a doc file, but I think this is a nice start. You can take a look at the docx module if you really want to write to a doc file.
EDIT: Here is an example python script.
with open("example.txt") as f:
content = f.readlines()
result = []
lengths = []
#Strip the file of \n
for line in content:
line = line.strip()
result.append(line)
lengths.append(len(line))
most_occuring = max(set(lengths), key=lengths.count)
bad_lines = []
for i in range(len(lengths)):
if (lengths[i] != most_occuring):
#Append the bad_line to bad_lines
bad_lines.append([i, result[i]])
#Check if it's a good, or a bad file
#if len(bad_lines == 0):
#Good File
#Move file to the good folder...
#else:
#Bad File
with open("bad_lines.txt", "wb") as f:
for bad_line in bad_lines:
f.write("[%3i] %s\n" % (bad_line[0], bad_line[1]))

Python 3.4.3: Iterating over each line and each character in each line in a text file

I have to write a program that iterates over each line in a text file and then over each character in each line in order to count the number of entries in each line.
Here is a segment of the text file:
N00000031,B,,D,D,C,B,D,A,A,C,D,C,A,B,A,C,B,C,A,C,C,A,B,D,D,D,B,A,B,A,C,B,,,C,A,A,B,D,D
N00000032,B,A,D,D,C,B,D,A,C,C,D,,A,A,A,C,B,D,A,C,,A,B,D,D
N00000033,B,A,D,D,C,,D,A,C,B,D,B,A,B,C,C,C,D,A,C,A,,B,D,D
N00000034,B,,D,,C,B,A,A,C,C,D,B,A,,A,C,B,A,B,C,A,,B,D,D
The first and last lines are "unusable lines" because they contain too many entries (more or less than 25). I would like to count the amount of unusable lines in the file.
Here is my code:
for line in file:
answers=line.split(",")
i=0
for i in answers:
i+=1
unusable_line=0
for line in file:
if i!=26:
unusable_line+=1
print("Unusable lines in the file:", unusable_line)
I tried using this method as well:
alldata=file.read()
for line in file:
student=alldata.split("\n")
answer=student.split(",")
My problem is each variable I create doesn't exist when I try to run the program. I get a "students" is not defined error.
I know my coding is awful but I'm a beginner. Sorry!!! Thank you and any help at all is appreciated!!!
A simplified code for your method using list,count and if condition
Code:
unusable_line = 0
for line in file:
answers = line.strip().split(",")
if len(answers) < 26:
unusable_line += 1
print("Unusable lines in the file:", unusable_line)
Notes:
Initially I have created a variable to store count of unstable lines unusable_line.
Then I iterate over the lines of the file object.
Then I split the lines at , to create a list.
Then I check if the count of list is less then 26. If so I increment the unusable_line varaiable.
Finally I print it.
You could use something like this and wrap it into a function. You don't need to re-iterate the items in the line, str.split() returns a list[] that has your elements in it, you can count the number of its elements with len()
my_file = open('temp.txt', 'r')
lines_count = usable = ununsable = 0
for line in my_file:
lines_count+=1
if len(line.split(',')) == 26:
usable+=1
else:
ununsable+=1
my_file.close()
print("Processed %d lines, %d usable and %d ununsable" % (lines_count, usable, ununsable))
You can do it much shorter:
with open('my_fike.txt') as fobj:
unusable = sum(1 for line in fobj if len(line.split(',')) != 26)
The line with open('my_fike.txt') as fobj: opens the file for reading and closes it automatically after leaving the indented block. I use a generator expression to go through all lines and add up all that have a length different from 26.

How to Iterate over readlines() in python

I am trying to add lines from a txt file to a python list for iteration, and the script wants to print every line and return an error. I'm using the readlines() function, but when I use list.remove(lines), it returns an error: File "quotes.py", line 20, in main list.remove(lines) TypeError: remove() takes exactly one argument (0 given).
def main():
while True:
try:
text_file = open("childrens-catechism.txt", "r")
lines = text_file.readlines()
# print lines
# print len(lines)
if len(lines) > 0:
print lines
list.remove(lines)
time.sleep(60)
else:
print "No more lines!"
break
text_file.close()
I can't see what I'm doing wrong. I know it has to do with list.remove(). Thank you in advance.
You can write in this way. It will save you some time and give you more efficiency.
import time
def main():
with open("childrens-catechism.txt", "r") as file:
for line in file:
print line,
time.sleep(60)
Try this as per your requirements, this will do what you need.
import time
def main():
with open("childrens-catechism.txt", "r") as file:
for lines in file.readlines():
if len(lines) > 0:
for line in lines:
print line
lines.remove(line)
else:
print "No more lines to remove"
time.sleep(60)
lines is a list here from your txt. files, and list.remove(lines) is not a correct syntax, you trying to delete a list on list. list is a function in Python. You can delete the elements in lines like;
del lines[0]
del lines[1]
...
or
lines.remove("something")
The logic is, remove() is deleting an element in a list, you have to write that list before remove() after then you have to write the thing that you want to delete in paranthesis of remove() function.
On opening a file, we can convert the file lines onto a list,
lines = list(open("childrens-catechism.txt", "r"))
From this list we can now remove entries with length greater than zero, like this,
for line in lines:
if len(line) > 0:
# do sth
lines.remove(line)
If you are trying to read all the lines from the file and then print them in order, and then delete them after printing them I would recommend this approach:
import time
try:
file = open("childrens-catechism.txt")
lines = file.readlines()
while len(lines) != 0:
print lines[0],
lines.remove(lines[0])
time.sleep(60)
except IOError:
print 'No such file in directory'
This prints the first line and then deletes it. When the first value is removed, the list shifts one up making the previous line (lines[1]) the new start to the list namely lines[0].
EDITED:
If you wanted to delete the line from the file as well as from the list of lines you will have to do this:
import time
try:
file = open("childrens-catechism.txt", 'r+') #open the file for reading and writing
lines = file.readlines()
while len(lines) != 0:
print lines[0],
lines.remove(lines[0])
time.sleep(60)
file.truncate(0) #this truncates the file to 0 bytes
except IOError:
print 'No such file in directory'
As far as deleting the lines from the file line for line I am not too sure if that is possible or efficient.

parse blocks of text from text file using Python

I am trying to parse some text files and need to extract blocks of text. Specifically, the lines that start with "1:" and 19 lines after the text. The "1:" does not start on the same row in each file and there is only one instance of "1:". I would prefer to save the block of text and export it to a separate file. In addition, I need to preserve the formatting of the text in the original file.
Needless to say I am new to Python. I generally work with R but these files are not really compatible with R and I have about 100 to process. Any information would be appreciated.
The code that I have so far is:
tmp = open(files[0],"r")
lines = tmp.readlines()
tmp.close()
num = 0
a=0
for line in lines:
num += 1
if "1:" in line:
a = num
break
a = num is the line number for the block of text I want. I then want to save to another file the next 19 lines of code, but can't figure how how to do this. Any help would be appreciated.
Here is one option. Read all lines from your file. Iterate till you find your line and return next 19 lines. You would need to handle situations where your file doesn't contain additional 19 lines.
fh = open('yourfile.txt', 'r')
all_lines = fh.readlines()
fh.close()
for count, line in enumerate(all_lines):
if "1:" in line:
return all_lines[count+1:count+20]
Could be done in a one-liner...
open(files[0]).read().split('1:', 1)[1].split('\n')[:19]
or more readable
txt = open(files[0]).read() # read the file into a big string
before, after = txt.split('1:', 1) # split the file on the first "1:"
after_lines = after.split('\n') # create lines from the after text
lines_to_save = after_lines[:19] # grab the first 19 lines after "1:"
then join the lines with a newline (and add a newline to the end) before writing it to a new file:
out_text = "1:" # add back "1:"
out_text += "\n".join(lines_to_save) # add all 19 lines with newlines between them
out_text += "\n" # add a newline at the end
open("outputfile.txt", "w").write(out_text)
to comply with best practice for reading and writing files you should also be using the with statement to ensure that the file handles are closed as soon as possible. You can create convenience functions for it:
def read_file(fname):
"Returns contents of file with name `fname`."
with open(fname) as fp:
return fp.read()
def write_file(fname, txt):
"Writes `txt` to a file named `fname`."
with open(fname, 'w') as fp:
fp.write(txt)
then you can replace the first line above with:
txt = read_file(files[0])
and the last line with:
write_file("outputfile.txt", out_text)
I always prefer to read the file into memory first, but sometimes that's not possible. If you want to use iteration then this will work:
def process_file(fname):
with open(fname) as fp:
for line in fp:
if line.startswith('1:'):
break
else:
return # no '1:' in file
yield line # yield line containing '1:'
for i, line in enumerate(fp):
if i >= 19:
break
yield line
if __name__ == "__main__":
with open('ouput.txt', 'w') as fp:
for line in process_file('intxt.txt'):
fp.write(line)
It's using the else: clause on a for-loop which you don't see very often anymore, but was created for just this purpose (the else clause if executed if the for-loop doesn't break).

Categories

Resources