Deleting a line if it starts with a specific number [duplicate] - python

This question already has answers here:
How to delete a specific line in a file?
(17 answers)
Closed 2 years ago.
Hey so right now I am trying to make a small program that can delete lines based off of number in front of the question. (Just so you don't have to retype the whole question again)
with open("DailyQuestions.txt", "r") as f:
lines = f.readlines()
with open("DailyQuestions.txt", "w") as w:
for line in lines:
Num, A = line.split(" - ")
if not line.startswith(Num):
w.write(line)
Textfile:
1 - Q1
2 - Q2
3 - Q3
4 - Q4
5 - Q5
The problem with this is that it either deletes the whole file or it it expects 2 values (Num, A = line.split(" - ")). I still can't figure out a way for it to just delete the whole line based on the number infront of it. Any tips or suggestions would help a lot!

Two things:
First, you can use one open by passing in r+, this can tidy up your code.
Secondly, w overwrites the entire file, it doesn't append. If you would like to make it append, you can pass in a instead of w to open. I recommend, assuming you aren't dealing with a large amount of data, storing what you want to write in a string variable (with newlines), and then writing that to the file.
Here is a solution:
toWrite = "";
with open("DailyQuestions.txt", "r") as f:
lines = f.readlines()
for line in lines:
Num, A = line.split(" - ")
if not Num == "Number to Replace":
toWrite += line
with open("DailyQuestions.txt", "w") as w:
w.write(toWrite)

As an option, read all lines from the file and the generator go through each, rewrite the file.
If you need to delete all lines where there is no number
with open('test_file.txt', 'r') as f:
#Delete line if not have -
lines = [value[value.find('-')+1:].strip() for value in f.readlines() if value.find('-') != -1]
with open('test_file.txt', 'w') as f:
if lines: f.write('\n'.join(lines))

Your primary problem lies in the construction of your Boolean Expression. Based on the current design of the program, the expression will always be False. Therefore nothing is outputted.
Before you search through all the lines of text, you need to create a variable to store the numbers that you want to exclude. Use a List or Set:
numbers_to_exclude = {'2', '4', '6', '5'}
Then utilize this variable in your Boolean Expression:
if first_character_of_line not in numbers_to_exclude:
Your final code should look something like:
numbers_to_exclude = {'2', '4', '6', '5'}
with open("DailyQuestions.txt", "r") as f:
lines = f.readlines()
with open("output.txt", "w") as w:
for line in lines:
first_char_in_line = line[0]
if first_char_in_line not in numbers_to_exclude: //The line begins with the number we want to output
w.write(line)

I dont really know how to code it but i can maybe to give you an idea.
My idea is to create all again by saving the text or all the line on a variable like a list and next rewrite it ( with a “for i in range(len()) :” loop for example) but with a “if” that took the first number and check if it is the number you want to delete, so you just have to dont write it.
The code will be not super optimize but i think that can work
I hope i can help you :)

If you are just trying to remove the lines from the file that starts with a specific number you could use this code:
rewrite = ""
exluded_num = 4 #Place your excluded number here
with open("DailyQuestions.txt", "r") as f:
for line in f:
Num, A = line.split(" - ")
if int(Num) != exluded_num:
rewrite += line
open("DailyQuestions.txt", "w").write(rewrite)
The problem with the code you posted was that you are altering the text file as you are trying to read through it. That will throw an exception.

Related

Reading strings and decreasing them

I just have a simple question when dealing with text files:
I have a text file and want to make a python program to read it and if it finds any number it replaces it by the number preceding it like if it finds 4 it replaces it with 3 so how can I do that?
The problem for me in this program is that python reads the numbers as strings, not integers, so it can't decrease or increase them.
out = open("out.txt", "w")
with open("Spider-Man.Homecoming.2017.1080p.BluRay.x264-[YTS.AG].txt", "r") as file:
lines = file.readlines()
for line in lines:
if line.isdigit():
out.write(str(int(line - 1)))
else:
out.write(line)
This code doesn't detect the numbers as numbers and I don't know why.
Putting #Samwise's comment together with your code:
with open("Spider-Man.Homecoming.2017.1080p.BluRay.x264-[YTS.AG].txt", "r") as file:
lines = file.readlines()
new_lines = []
for line in lines:
decreased = ''.join(str(int(c)-1) if c.isdigit() else c for c in line)
new_lines.append(decreased)
with open('out.txt', 'w') as out:
out.writelines(new_lines)
You also should close the file after writing to it, so switched to with open at the end as a better way to write to file.

Dividing a .txt file in multiple parts in Python

I'm a begginer in Python, and I have a question about file reading :
I need to process info in a file to write it in another one. I know how to do that, but it's reaaally ressource-consuming for my computer, as the file is really big, but I know how it's formatted !
The file follows that format :
4 13
9 3 4 7
3 3 3 3
3 5 2 1
I won't explain what it is for, as it would take ages and would not be very useful, but the file is essentialy made of four lines like these, again and again. For now, I use this to read the file and convert it in a very long chain :
inputfile = open("input.txt", "r")
output = open("output.txt", "w")
Chain = inputfile.read()
Chain = Chain.split("\n")
Chained = ' '.join(Chain)
Chain = Chained.split(" ")
Chain = list(map(int, Chain))
Afterwards, I just treat it with "task IDs", but I feel like it's really not efficient.
So do you know how I could divide the chain into multiple ones knowing how they are formatted?
Thanks for reading !
How about:
res = []
with open('file', 'r') as f:
for line in f:
for num in line.split(' '):
res.append(int(num))
Instead of reading the whole file into memory, you go line by line.
Does this help?
If you need to go 4 lines at a time, just add an internal loop.
Regarding output, I'm assuming you want to do some computation on the input, so I wouldn't necessarily do this in the same loop. Either process the input once reading is done, or instead of using a list, use a queue and have another thread read from the queue while this thread is writing to it.
Perhaps the utility of a list comprehension will help a bit as well (I doubt this will make an impact):
res = []
with open('file', 'r') as f:
for line in f:
res.append( int(num) for num in line.split() )
hmm there's some method to write to a file without reading it i believe
Add text to end of line without loading file
https://docs.python.org/2.7/library/functions.html#print
from __future__ import print_function
# if you are using python2.7
i = open("input","r")
f = open("output.txt","w")
a = "awesome"
for line in i:
#iterate lines in file input
line.strip()
#this will remove the \n in the end of the string
print(line,end=" ",file=f)
#this will write to file output with space at the end of it
this might help, i'm a newbie too, but with better google fu XD
Maybe do it line by line. This way it consumes less memory.
inputfile = open("input.txt", "r")
output = open("output.txt", "a")
while True:
line = inputfile.readline()
numbers = words.split(" ")
integers = list(map(int, numbers))
if not line:
break
There is probably a newline character \n in the words. You should also replace that with an empty string.
If you don't wanna to consume memory (you can run of it if file is very large), you need to read lien by line.
with open('input.txt', 'w') as inputfile, open('"output.txt', 'w') as output:
for line in inputfile:
chain = line.split(" ")
#do some calculations or what ever you need
#and write those numbers to new file
numbers = list(map(int, chain))
for number in numbers
output.write("%d " % number)

Python 3 How to add specific lines from a list to an array

Below is my code. This code reads lines from a file (called compsc), strips the \n from them, puts them into an array and randomly prints them, eliminating each option that has already been printed. What I want to know is how to read only a specific set of lines into the array, as I will have lots of lines in the .txt file. So, is there some code that can do that, or do I have to put readlines() somewhere?
Thanks in advance!
import random
with open("compsc.txt", "r") as ins:
qarray = []
for line in ins:
line = line.strip()
qarray.append(line)
print (qarray)
loop = 0
while loop != 4:
newquestion = random.sample(qarray, 1)
print (newquestion)
qarray.remove(newcard[0])
loop = loop + 1
You will need to create some function to decide whether or not to keep the line.
import random
def line_filter(line):
"""Return True if you want to keep line, False otherwise."""
...
with open("compsc.txt", "r") as f:
questions = [line.strip() for line in f if line_filter(line)]
random.shuffle(questions)
for question in questions[:4]:
print(question)
This has been covered on this site before. In brief, if your file is not huge i.e. does not cause memory problems you could indeed use readlines. Also look into linecache, which is optimized.

Python- how to use while loop to return longest line of code

I just started learning python 3 weeks ago, I apologize if this is really basic. I needed to open a .txt file and print the length of the longest line of code in the file. I just made a random file named it myfile and saved it to my desktop.
myfile= open('myfile', 'r')
line= myfile.readlines()
len(max(line))-1
#the (the "-1" is to remove the /n)
Is this code correct? I put it in interpreter and it seemed to work OK.
But I got it wrong because apparently I was supposed to use a while loop. Now I am trying to figure out how to put it in a while loop. I've read what it says on python.org, watched videos on youtube and looked through this site. I just am not getting it. The example to follow that was given is this:
import os
du=os.popen('du/urs/local')
while 1:
line= du.readline()
if not line:
break
if list(line).count('/')==3:
print line,
print max([len(line) for line in file(filename).readlines()])
Taking what you have and stripping out the parts you don't need
myfile = open('myfile', 'r')
max_len = 0
while 1:
line = myfile.readline()
if not line:
break
if len(line) # ... somethin
# something
Note that this is a crappy way to loop over a file. It relys on the file having an empty line at the end. But homework is homework...
max(['b','aaa']) is 'b'
This lexicographic order isn't what you want to maximise, you can use the key flag to choose a different function to maximise, like len.
max(['b','aaa'], key=len) is 'aaa'
So the solution could be: len ( max(['b','aaa'], key=len) is 'aaa' ).
A more elegant solution would be to use list comprehension:
max ( len(line)-1 for line in myfile.readlines() )
.
As an aside you should enclose opening a file using a with statement, this will worry about closing the file after the indentation block:
with open('myfile', 'r') as mf:
print max ( len(line)-1 for line in mf.readlines() )
As other's have mentioned, you need to find the line with the maximum length, which mean giving the max() function a key= argument to extract that from each of lines in the list you pass it.
Likewise, in a while loop you'd need to read each line and see if its length was greater that the longest one you had seen so far, which you could store in a separate variable and initialize to 0 before the loop.
BTW, you would not want to open the file with os.popen() as shown in your second example.
I think it will be easier to understand if we keep it simple:
max_len = -1 # Nothing was read so far
with open("filename.txt", "r") as f: # Opens the file and magically closes at the end
for line in f:
max_len = max(max_len, len(line))
print max_len
As this is homework... I would ask myself if I should count the line feed character or not. If you need to chop the last char, change len(line) by len(line[:-1]).
If you have to use while, try this:
max_len = -1 # Nothing was read
with open("t.txt", "r") as f: # Opens the file
while True:
line = f.readline()
if(len(line)==0):
break
max_len = max(max_len, len(line[:-1]))
print max_len
For those still in need. This is a little function which does what you need:
def get_longest_line(filename):
length_lines_list = []
open_file_name = open(filename, "r")
all_text = open_file_name.readlines()
for line in all_text:
length_lines_list.append(len(line))
max_length_line = max(length_lines_list)
for line in all_text:
if len(line) == max_length_line:
return line.strip()
open_file_name.close()

Two simple questions about python

I have 2 simple questions about python:
1.How to get number of lines of a file in python?
2.How to locate the position in a file object to the
last line easily?
lines are just data delimited by the newline char '\n'.
1) Since lines are variable length, you have to read the entire file to know where the newline chars are, so you can count how many lines:
count = 0
for line in open('myfile'):
count += 1
print count, line # it will be the last line
2) reading a chunk from the end of the file is the fastest method to find the last newline char.
def seek_newline_backwards(file_obj, eol_char='\n', buffer_size=200):
if not file_obj.tell(): return # already in beginning of file
# All lines end with \n, including the last one, so assuming we are just
# after one end of line char
file_obj.seek(-1, os.SEEK_CUR)
while file_obj.tell():
ammount = min(buffer_size, file_obj.tell())
file_obj.seek(-ammount, os.SEEK_CUR)
data = file_obj.read(ammount)
eol_pos = data.rfind(eol_char)
if eol_pos != -1:
file_obj.seek(eol_pos - len(data) + 1, os.SEEK_CUR)
break
file_obj.seek(-len(data), os.SEEK_CUR)
You can use that like this:
f = open('some_file.txt')
f.seek(0, os.SEEK_END)
seek_newline_backwards(f)
print f.tell(), repr(f.readline())
Let's not forget
f = open("myfile.txt")
lines = f.readlines()
numlines = len(lines)
lastline = lines[-1]
NOTE: this reads the whole file in memory as a list. Keep that in mind in the case that the file is very large.
The easiest way is simply to read the file into memory. eg:
f = open('filename.txt')
lines = f.readlines()
num_lines = len(lines)
last_line = lines[-1]
However for big files, this may use up a lot of memory, as the whole file is loaded into RAM. An alternative is to iterate through the file line by line. eg:
f = open('filename.txt')
num_lines = sum(1 for line in f)
This is more efficient, since it won't load the entire file into memory, but only look at a line at a time. If you want the last line as well, you can keep track of the lines as you iterate and get both answers by:
f = open('filename.txt')
count=0
last_line = None
for line in f:
num_lines += 1
last_line = line
print "There were %d lines. The last was: %s" % (num_lines, last_line)
One final possible improvement if you need only the last line, is to start at the end of the file, and seek backwards until you find a newline character. Here's a question which has some code doing this. If you need both the linecount as well though, theres no alternative except to iterate through all lines in the file however.
For small files that fit memory,
how about using str.count() for getting the number of lines of a file:
line_count = open("myfile.txt").read().count('\n')
I'd like too add to the other solutions that some of them (those who look for \n) will not work with files with OS 9-style line endings (\r only), and that they may contain an extra blank line at the end because lots of text editors append it for some curious reasons, so you might or might not want to add a check for it.
The only way to count lines [that I know of] is to read all lines, like this:
count = 0
for line in open("file.txt"): count = count + 1
After the loop, count will have the number of lines read.
For the first question there're already a few good ones, I'll suggest #Brian's one as the best (most pythonic, line ending character proof and memory efficient):
f = open('filename.txt')
num_lines = sum(1 for line in f)
For the second one, I like #nosklo's one, but modified to be more general should be:
import os
f = open('myfile')
to = f.seek(0, os.SEEK_END)
found = -1
while found == -1 and to > 0:
fro = max(0, to-1024)
f.seek(fro)
chunk = f.read(to-fro)
found = chunk.rfind("\n")
to -= 1024
if found != -1:
found += fro
It seachs in chunks of 1Kb from the end of the file, until it finds a newline character or the file ends. At the end of the code, found is the index of the last newline character.
Answer to the first question (beware of poor performance on large files when using this method):
f = open("myfile.txt").readlines()
print len(f) - 1
Answer to the second question:
f = open("myfile.txt").read()
print f.rfind("\n")
P.S. Yes I do understand that this only suits for small files and simple programs. I think I will not delete this answer however useless for real use-cases it may seem.
Answer1:
x = open("file.txt")
opens the file or we have x associated with file.txt
y = x.readlines()
returns all lines in list
length = len(y)
returns length of list to Length
Or in one line
length = len(open("file.txt").readlines())
Answer2 :
last = y[-1]
returns the last element of list
Approach:
Open the file in read-mode and assign a file object named “file”.
Assign 0 to the counter variable.
Read the content of the file using the read function and assign it to a
variable named “Content”.
Create a list of the content where the elements are split wherever they encounter an “\n”.
Traverse the list using a for loop and iterate the counter variable respectively.
Further the value now present in the variable Counter is displayed
which is the required action in this program.
Python program to count the number of lines in a text file
# Opening a file
file = open("filename","file mode")#file mode like r,w,a...
Counter = 0
# Reading from file
Content = file.read()
CoList = Content.split("\n")
for i in CoList:
if i:
Counter += 1
print("This is the number of lines in the file")
print(Counter)
The above code will print the number of lines present in a file. Replace filename with the file with extension and file mode with read - 'r'.

Categories