reading individual line in a file with python - python

How is this wrong? It seems like I am doing this right but every time. I have tried changing the readline part to read but that didn't work.
Here is my code:
f = open("pg1062.txt","r").read()
print f.readline(1)
print f.readline(2)
print f.readline(3)
Here is the error I get:
print f.readline(1)
AttributeError: 'str' object has no attribute 'readline'

This uses a loop to print your lines.
f = open("pg1062.txt", 'r')
while True:
line = f.readline()
if line == "":
break
print(line)
If you want to only print a specific number of lines, then do something like this:
f = open("pg1062.txt", 'r')
count = 1
while count < 4:
line = f.readline()
if line == "":
break
print(line)
count += 1

Your problem is at this line
f = open("pg1062.txt","r").read()
just remove .read() and your problem will be fixed. Your final code should look like.
f = open("pg1062.txt","r")
print f.readline()
print f.readline()
print f.readline()
And if you want to print all lines from text file, see code below
f = open("pg1062.txt","r")
for line in f:
print line

This is certainly a duplicate. At any rate, anything above Python 2.4 should use a with block.
with open("pg1062.txt", "r") as fin:
for line in fin:
print(line)
If you happen to want them in a list:
with open("pg1062.txt", "r") as fin:
lines = [line for line in fin] # keeps newlines at the end
lines = [line.rstrip() for line in fin] # deletes the newlines
or more or less equivalently
with open("pg1062.txt", "r") as fin:
lines = fin.readlines() # keeps newlines at the end
lines = fin.read().splitlines() # deletes the newlines

Related

Not all duplicates are deleted from a text file in Python

I am new to Python. I am trying to delete duplicates from my text file by doing the following:
line_seen = set()
f = open('a.txt', 'r')
w = open('out.txt', 'w')
for i in f:
if i not in line_seen:
w.write(i)
line_seen.add(i)
f.close()
w.close()
In the initial file I had
hello
world
python
world
hello
And in output file I got
hello
world
python
hello
So it did not remove the last duplicate. Can anyone help me to understand why it happened and how could I fix it?
The first line probably contains 'hello\n' - the last line contains only 'hello' - they are not the same.
Use
line_seen = set()
with open('a.txt', 'r') as f, open('out.txt', 'w') as w:
for i in f:
i = i.strip() # remove the \n from line
if i not in line_seen:
w.write(i + "\n")
line_seen.add(i)
The main problem is with the break line characters ("\n") which appears at the end of each line but the last line. You can use a combination of set, map and join function such as what follows:
f = open('a.txt', 'r')
w = open('out.txt', 'w')
w.write("\n".join(list(set(map(str.strip,f.readlines())))))
out.txt
python
world
hello
If you want to stick to your previous approach you can use:
line_seen = set()
f = open('a.txt', 'r')
w = open('out.txt', 'w')
for i in f:
i = i.strip()
if i not in line_seen:
w.write(i)
line_seen.add(i)
f.close()
w.close()
Most likely you didn't end the last line with a newline. The known line is `hello\n'. The last just 'hello'
Fix the input or strip() the read i
# Since we check if the line exists in lines, we can use a list instead of
# a set to preserve order
lines = []
infile = open('a.txt', 'r')
outfile = open('out.txt', 'w')
# Use the readlines method
for line in infile.readlines():
if line not in lines:
# Strip whitespace
line = line.strip()
lines.append(line)
for line in lines:
# Add the whitespace back
outfile.write("{}\n".format(line))
infile.close()
outfile.close()

How to read array from other file?

I have a file (list.txt) with huge array like this:
['1458575', '1458576', '1458577', '1458578'...]
I want to read it in my script but the output is white. The goal is print the number of line (list.txt) of the file.txt
numbers_line = open("list.txt", "r")
lines = numbers_line.readlines().split(',')
i=0
f=open('file.txt')
for line in f:
if i in lines:
print (line)
i+=1
However, if I put the array direct it's read, but considering that is a huge array this is not could be helpful.
lines=['1458575', '1458576', '1458577', '1458578', '1458579', '1458580', '1458581', '1458582', '1458583', '1458584']
i=0
f=open('file.txt')
for line in f:
if i in lines:
print (line)
i+=1
Thanks for your support
readlines returns list of lines, not string
readline will return a string, but split(',') would give a list of string not int
try this
import ast
text_file = open("list.txt", "r")
lines = list(map(int, ast.literal_eval(text_file.read().strip())))
i=0
f=open('file.txt')
for line in f:
if i in lines:
print (line)
i+=1
This can work
with open('file.txt') as file:
content = file.read()
a = []
exec('a = ' + content) # executes - a = ['1458575', ...]
Alternate method - copy file.txt to new file my_lists.py.
Add 'a = ' at the start of my_lists.py file and execute
from my_lists import a
Use with open() as : instead of open() and close(), because it closes the connection to file automatically when errors occurs while the traditional manual open() and close() don't do.
lines=['1458575', '1458576', '1458577', '1458578', '1458579', '1458580', '1458581', '1458582', '1458583', '1458584']
lines = [int(x) for x in lines] # convert to integers
fpath = "list.txt"
with open(fpath, "r") as f:
for x in f:
x = x.trim() # remove end of line character
if int(x) in lines:
print(x)

Python: Print x lines after a pattern including the line with pattern

If a line starts with a number(xyz) in a file, I need to print(or write to a file) this line and the next xyz+1 lines.
What's the best way to do this?
So far, I've been able to print the line that starts with an int. How do I print the next lines?
import glob, os, sys
import subprocess
file = 'filename.txt'
with open(file,'r') as f:
data = f.readlines()
for line in data:
if line[0].isdigit():
print int(line)
If I made an iterator out of data, the print function skips a line every time.
with open(file,'r') as f:
data = f.readlines()
x = iter(data)
for line in x:
if line[0].isdigit():
print int(line)
for i in range(int(line)):
print x.next()
How could I make it stop skipping lines?
Use a flag, when you find the line set it to true, then use it to write all future lines:
can_write = False
with open('source.txt') as f, open('destination.txt', 'w') as fw:
for line in f:
if line.startswith(xyz):
can_write = True
if can_write:
fw.write(line)

skip 2 lines after a match while reading from a file

I am trying to code something where I first look for some string in a line in a txt file and when it is found I want to skip that row and the row below to get a new txt file without those rows. I really didn't get any solution from other questions here so maybe this will work
My code looks like this now:
with open("bla.txt", "r+") as f
new_f = f.readlines()
f.seek(0)
for line in new_f:
if "abc" not in line:
f.write(line)
else:
pass
pass
f.truncate()
I tried it with next(f) aswell but it didn't work for me. thanks in advance
This code creates a new file that skip the current and next row if the current row has the string ABC:
with open('bla.txt','r') as f:
text = f.read()
lines = text.split('\n')
with open('new_file.txt','w') as nf:
l = 0
while l<(len(lines)):
if 'ABC' in lines[l]:
l = l+2
else:
nf.write(lines[l]+'\n')
l = l+1
Try something simple like this:
import os
search_for = 'abc'
with open('input.txt') as f, open('output.txt', 'w') as o:
for line in f:
if search_for in line:
next(f) # we need to skip the next line
# since we are already processing
# the line with the string
# in effect, it skips two lines
else:
o.write(line)
os.rename('output.txt', 'input.txt')
Here is a repl with sample code.

Printing a line after another from a text file in loop with delay

I want the program to print the lines in the .txt one after another
import os
import time
with open('e.txt','rt') as f:
#for line in f: # for direct input
line = f.readline()
m = str(line)
print 'd', m
time.sleep(2)
line = f.readline()
g = str(line)
print 'f', g
As you see there are two lines so printing them this way works fine, but when i want to use a loop
with open('e.txt','rt') as f:
for i, l in enumerate(f):
pass
d = i + 1
while d > 0 :
#with open('e.txt','rt') as f:
pos = f.tell();
f.seek(pos,0);
line=f.readline();
m = str(line);
time.sleep(1)
print 't: ', m
d -= 1
the output is
t:
t:
i dont understand what am i doing wrong please help
also thanks in advance.
It seems like you are overdoing it, you can do it pretty simply like so:
import time
f = open('e.txt','r')
for line in f.readlines():
print(line)
time.sleep(1)
That's it....
P.S. you dont need rt in the open as the t mode is default.
EDIT: the problem with your program is that you were trying to print an object because when you write
f = open(...)
or
with open(...) as f
f is a file object, you cant iterate over it, you can however iterate over f.readlines which returns a list of the lines in the file as text.
open the file, iterate the file object.
file = open( 'e.txt', 'r')
for line in file:
print(line)
"i want the program to print the lines in the .txt one after another" in its simplest form corresponds to:
with open('e.txt','r') as f:
for line in f:
print(line.strip())
A delay is a matter of choice, not a necessity to solve the original request. The .strip command is there to remove any newline characters which will produce a space between your lines.

Categories