Receursivley read lines of a file - python-3.x

I want to read specific lines of a file each time I run the program.
Say a text file has
line 1
line 2
line 3
line 4
line 5
line 6
I would like to read every third line of the file, every time I run it.
with open("test.txt", "r+") as f:
for line in f:
line = f.readline()
#do something with line
The above code only reads the first line or it just picks which line to read and I want to be specific. Every third line.
Enumerate does allow for me to read the specific lines but evaluating them one by one is the problem thereafter.
How do I do it?

Here is an example:
def _read(file, step, start=0):
with open(file, "r") as f:
for line in f.readlines()[start::step]:
print(line.strip())
_read('test.txt', 3, 2)
Output:
line 3
line 6
line 9
Here is with class:
class Reader:
def __init__(self, file_name, mode='r', start=0, step=1):
self.file = open(file_name, mode)
self.lines = self.file.readlines()[start::step]
self.file.close()
def get_line(self):
if self.lines:
return self.lines.pop(0).strip()
else:
print('No more lines')
r = Reader('test.txt', start=2, step=3)
print(r.get_line())
print(r.get_line())
print(r.get_line())
print(r.get_line())
print(r.get_line())
Output:
line 3
line 6
line 9
No more lines
None
No more lines
None

Related

python3 replace a empty line with fileinput

I have a file that has empty line between each line like this:
line one
line two
line three
I want to append a line after line two
This is how I am trying to do it
for line_num, line in enumerate(fileinput.FileInput(file, inplace=1) ):
if line_num == 4:
line.replace(" ", 'line to append')
The problem with this is it overwrites the entire file when I want to append the line
Your approach has a few (small) issues.
you do not print the lines to the file
line_num == 4 will not write after line two, unless you use enumerate with start=1
(assuming that you want to fill an empty line) line.replace(' ', 'line to append') will not do the trick because an empty line does contain a space
Try:
>>> import fileinput
>>> with fileinput.input('test', inplace=True) as f:
... for line_num, line in enumerate(f):
... if line_num == 3 and line in ['\n', '\r\n']:
... line = 'line to append\n'
... print(line, end='')
...
test had originally the content in your description and after:
$ cat test
line one
line two
line to append
line three
line_num == 3 and line in ['\n', '\r\n'] says "find the 4th line and check if it is empty". You may want to update/replace the checks based on your needs.

Reading each line from text file

I have a script which reads each line from text file. but somehow it prints all at once. I want to run one line end and run next. here is the code.
f = open('textfile.txt', 'r')
file= f.read()
for x in file:
print(x, file.strip())
comSerialPort.write(x.encode('utf-8'))
Use readlines instead of read
with open('textfile.txt', 'r') as f:
lines = f.readlines()
for line in lines:
print(line)
# do stuff with each line
Use with statement and then iterate lines.
Ex:
with open('textfile.txt', 'r') as infile:
for line in infile:
print(line)
comSerialPort.write(line.strip().encode('utf-8'))
Note: read() reads the entire content of the file.

Count frequency of words under given index in a file

I am trying to count occurrence of words under specific index in my file and print it out as a dictionary.
def count_by_fruit(file_name="file_with_fruit_data.txt"):
with open(file_name, "r") as file:
content_of_file = file.readlines()
dict_of_fruit_count = {}
for line in content_of_file:
line = line[0:-1]
line = line.split("\t")
for fruit in line:
fruit = line[1]
dict_of_fruit_count[fruit] = dict_of_fruit_count.get(fruit, 0) + 1
return dict_of_fruit_count
print(count_by_fruit())
Output: {'apple': 6, 'banana': 6, 'orange': 3}
I am getting this output, however, it doesn't count frequency of the words correctly. After searching around I didn't seem to find the proper solution. Could anyone help me to identify my mistake?
My file has the following content: (data separated with tabs, put "\t" in example as format is being altered by stackoverflow)
I am line one with \t apple \t from 2018
I am line two with \t orange \t from 2017
I am line three with \t apple \t from 2016
I am line four with \t banana \t from 2010
I am line five with \t banana \t from 1999
You are looping too many times over the same line. Notice that the results you are getting are all 3 times what you are expecting.
Also, in Python, you also do not need to read the entire file. Just iterate over the file object line by line.
Try:
def count_by_fruit(file_name="file_with_fruit_data.txt"):
with open(file_name, "r") as f_in:
dict_of_fruit_count = {}
for line in f_in:
fruit=line.split("\t")[1]
dict_of_fruit_count[fruit] = dict_of_fruit_count.get(fruit, 0) + 1
return dict_of_fruit_count
Which can be further simplified to:
def count_by_fruit(file_name="file_with_fruit_data.txt"):
with open(file_name) as f_in:
dict_of_fruit_count = {}
for fruit in (line.split('\t')[1] for line in f_in):
dict_of_fruit_count[fruit] = dict_of_fruit_count.get(fruit, 0) + 1
return dict_of_fruit_count
Or, if you can use Counter:
from collections import Counter
def count_by_fruit(file_name="file_with_fruit_data.txt"):
with open(file_name) as f_in:
return dict(Counter(line.split('\t')[1] for line in f_in))
The problem is for fruit in line:. Splitting the lines on the tabs is going to split them into three parts. If you loop over those three parts every time, adding one to the count for each, then your counts are going to be 3 times as large as the actual data.
Below is how I would write this function, using generator expressions and Counter.
from collections import Counter
def count_by_fruit(file_name="file_with_fruit_data.txt"):
with open(file_name, "r") as file:
lines = (line[:-1] for line in file)
fruit = (line.split('\t')[1] for line in lines)
return Counter(fruit)

How to print 2 lines under a line from a text file?

Edit : I want to print 2 lines under the code entered by the user but it doesn't seem to work.
my text file looks like this :
86947367
banana
5
78364721
apple
3
35619833
orange
2
84716491
sweets
8
46389121
chicken
10
I have tried :
file = ('read_it.txt')
user = input('Enter code')
with open(file, 'r') as f:
lines = f.readlines()
for i, line in enumerate(lines):
if line == user:
print("{}\n{}".format(lines[i+1], lines[i+2]))
But i get an output of 2 blank lines.
file = 'filename.txt'
user = input('Enter code')
with open(file, 'r') as f:
lines = [line.strip() for line in f.readlines()] # Strip \n and \t from text
for i, line in enumerate(lines): # enumerate will count and keep track of the lines
if line == user:
print("{}\n{}".format(lines[i+1], lines[i+2]))

keep order as it was saved and keep only unique words in text file list

need sort lines in order in which they were saved in txt file, just new line comes from below and save this order after remove similar words. so if I add words in loop one by one
line A
line B
line C
line D
line E
here I got three solutions, but nothing works for me correct
first keeps only unique words;
with open('C:\my_path\doc.txt', 'r') as lines:
lines_set = {line.strip() for line in lines}
with open(''D:\path\file.txt', 'w') as out:
for line in lines_set:
out.write(line + '\n')
but destroys order:
1. line B
2. line E
3. line C
4. line D
5. line A
second keeps order but same words too:
with open('C:\my_path\doc.txt', 'r') as lines:
lines_set = []
for line in lines:
if line.strip() not in lines_set:
lines_set.append(line.strip())
last one works well, but with input text:
with open('C:\my_path\doc.txt', 'r') as lines:
lines_set = []
for line in lines:
if line.strip() not in lines_set:
lines_set.append(line.strip())
in some cases I have no any input, and also have different input, so need somehow sort ordered list itself
can you help me figure out with it please
loadLines is almost as your function you show twice, but it allows duplicates. removeDuplicates strips duplicates. saveLines writes a list to a file, deliminating by newline. All functions preserve order.
#Load lines with duplicates
def loadLines(f):
with open(f, 'r') as lines:
lines_set = []
for line in lines:
lines_set.append(line.strip())
return lines_set
#Search list "l", return list without duplicates.
def removeDuplicates(l):
out = list(set(l))
for i in enumerate(out):
out[i[0]] = l.index(i[1])
out.sort()
for i in enumerate(out):
out[i[0]] = l[i[1]]
return out
#Write the lines "l" to filepath "f"
def saveLines(f, l):
open(f, 'w').write('\n'.join(l))
lines = loadLines('doc.txt')
print(lines)
stripped_lines = removeDuplicates(lines)
print(stripped_lines)
saveLines('doc.txt', stripped_lines)

Resources