How to print 2 lines under a line from a text file? - python-3.x

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]))

Related

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)

Receursivley read lines of a file

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

Deleting a line from txt file

I have a txt file with lists of names like this
Name1
Name2
Name3
I want to delete the line with "Name2" in it, if Name2 is in the list. I got this code:
f = open(list,'r')
if("Name2" in f.read().splitlines()):
lines = f.readlines()
f.close()
f = open(badhumanslist, "w")
for line in lines:
if line != "Name2" + "\n":
f.write(line)
f.close()
The problem is that this code empties the whole file. I don't see my error, it should rewrite all the lines, except the one with "Name2"
You already read the whole file in line 2: f.read(). Then, lines = f.readlines() returns an empty list.
def read_all():
with open(filename, "r") as file:
return file.read()
content = read_all()
lines = content.splitlines()
if "Name2\n" in lines:
with open(filename, "w") as file:
for line in lines:
if line != "Name2\n":
file.write(line)

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)

How do I split a list of first and last names apart?

I'm trying to print only the first names from a .txt list into a new text file.
The .txt list looks like this (each pair of names on its own line, with many more names than shown here):
Smith, James
Doe, John
White, Susan
Here's my current code:
fname = input("Please enter the file name for your list of names: ")
infile = open(fname,"r")
data = infile.read()
outfile = open("student-firstnames.txt", "w")
first = data.split(",")[-1]
print(first, file=outfile)
print("First names have been written to student-firstnames.txt.")
This gives me only the very last first name, but I want an entire list of only first names. What can I do to achieve this?
You could try the following:
first = [i.split(", ")[1] for i in data.split("\n")]
print(first, file=outfile)
This splits the data by newline and then gets the part after the comma, which is the first name.
You can iterate over the file object to process each line of the file one at a time:
with open(fname) as infile:
for line in infile:
...
For example, you could use this to get a list of the first names for each line in the file:
with open(fname) as infile:
first_names = [line.strip('\n').split(', ')[1] for line in infile]
In Python 3 you could do it like this:
fname = input("Please enter the file name for your list of names: ")
with open(fname, "r") as infile, open("student-firstnames.txt", "w") as outfile:
for line in infile:
firstname = line.strip().split(', ')[-1]
print(firstname, file=outfile)
You could try this also to get the names at index 1.
fname = input("Please enter the file name for your list of names: ")
infile = open(fname,"r")
outfile = open("student-firstnames.txt", "w")
x = '\n,'.join([line.split(', ')[1].strip() for line in infile])
outfile.write(x)
outfile.close()
infile.close()
Output:
James
,John
,Susan

Resources