How to compare lines from a text file in Python? - python

hope this is not a trivial question since I'm new to Python.
I have a text file, and I need to compare the first line with all the following ones, than the second line with all the following ones, etc.
If it was a list I'd just make two for loops, but I don't know how to start reading a file from the line after the one that needs to be compared.
Could someone help me?

You can get a list with each line as an element using a readlines() function.
with open(file_name) as file:
lines_list = f.readlines()

This will give you a list of all the lines:
with open(path_to_file) as f:
list_of_lines = f.readlines()

for line in f1:
for line_aux in f2:
compare the two lines

Related

Python: Remove first instance only of string from text file

I have been removing the desired lines from a text file by reading it in and rewriting each line if the string(s) I wish to remove are not present, using the following.
with open('infile.txt', 'r') as f:
lines = f.readlines()
with open('outfile.txt', 'w+') as f:
for line in lines:
if line.strip("\n") != "Desired text on line to remove":
f.write(line)
This works fine for all but one of the lines I need to remove which only contains.
1.
This is the first instance of (1.) in the file, and always will be in the files I'm editing; however it is repeated later in the text file and these later instances must be kept - is it possible to remove only the first instance of this text?
If I've understood your question correctly then you want to get rid of first '1.' and keep the rest. It can be done in multiple ways, like below code.
with open('infile.txt', 'r') as f:
lines = f.readlines()
with open('outfile.txt', 'w+') as f:
t = '1.'
for line in lines:
line = line.strip("\n")
if line != "Desired text on line to remove" and line != t:
f.write(line)
f.write("\n")
if line == t:
t = None
One of them is by simply using logical operators (which I've used) and create a variable you want to remove. Which in my case as t. Now use it to filter the first instance. Thereafter change its value to None so that in the next instance it will always be a True statement and the condition to run or not depends on if line is equal to our desired text or not.
P.S.- I've added some more line of codes like line = line.strip("\n") and f.write("\n") just to make the output and code clearer. You can remove it if you want as they don't contribute to clear the hurdle.
Moreover, if you don't get the desired output or my code is wrong. Feel free to point it out as I've not written any answers yet and still learning.

Why I can't read the first line of the file?

When I read line by line from the file, the first line is skipped. Why does this happen and How can I solve this?
web="www.instagram.com/"
f= open("accounts.txt","r")
ft=open("add.txt", "w")
links=[]
for l in f:
name=f.readlines()
print(name)
fullweb=web+name
links.append(fullweb)
ft.writeline(fullweb)
f.close()
ft.close()
print(links)
You were starting iterating over the file f (as iterator)
for l in f:
getting the 1st line and then, immediately read all its remaining contents with f.readlines().
That makes no big sense (if not saying - no sense).
You should either do for l in f or f.readlines, not both!
with open("accounts.txt") as f:
lines = f.readlines()
...
or
with open("accounts.txt") as f:
for line in f:
...
Explanation:
With the iterator, you already iterate over the lines in the file. So it already "eats up" the first line, than you reach "readlines" readlines reads the other lines of the file. So doing both, is plainly not helpful.
I think I should have used the iteration variable instead of calling the function to readline().Anyway this solved the problem.
for l in f:
print(l)
fullweb=web+l
links.append(fullweb)
ft.writelines(fullweb)
But I don't know the theory behind how the for loop reads the file line by line without any function.

Python: Create an array of substrings from an array

I grabbed an array from the lines of a txt file. The problem is I only need the end of these lines. I'm still pretty new to Python so I'm not sure how to put these concepts together. Below is my best attempt, the first part works but the second falls apart.
with open("prices.txt") as f:
readline = f.readlines()
for seatCapacity in readline:
seatCapacity = readline[10:]
I guess, you want like this.
with open("prices.txt") as f:
for line in f:
seatCapacity = line[10:]
you can refer this solution: How should I read a file line-by-line in Python?

converting a string to a list from a text file

with open('task3randomtext.txt', 'r') as f:
text = f.readlines()
this is the code I use to take text from a text file and read it into my program but it reads it in as a list an will not let me split it to convert it to a array. Is there any other way or splitting it or reading it in.
If you look at the Python documentation (which you should!) you can see that readlines() returns a list containing every line in the file. To then split each line, you could do something along the lines of:
with open('times.txt') as f:
for line in f.readlines():
print line.split()

In python, how can you delete the third forth and fifth line of a text file?

There was another question about deleting the first three and the last line, they used .writelines(), but that only work if you want to delete the outside lines, not delete lines in the the middle.
Any ways of deleting multiple specific lines from anywhere in a text file in python?
Please, Please, PLease Help!!
Try following code:
with open('input.txt') as f, open('output.txt', 'w') as fout:
fout.writelines(line for lineno, line in
enumerate(f, 1) if lineno not in (3,4,5))

Categories

Resources