How to read a specific word in a text file? [duplicate] - python

This question already has answers here:
How to count word "test" in file on Python?
(5 answers)
Closed 2 years ago.
I am trying to make this program read a specific word in a text file, but the outcome only comes as "1". Why is this happening?
import os
openfile = input('Enter the input file: ')
accumulator = 0
entry = "PMID"
if os.path.isfile(openfile):
file = open(openfile,'r')
for entry in file.readlines():
accumulator +=1
print('there are:',accumulator)
exit()
print('Input file not found.')
print('Please check the file name or the location of your input file.')
Thank you so much!

If you are looking to count how many times a certain word comes up we can use python's built-in str.count() function in order to do so. See documentation here:
https://docs.python.org/3/library/stdtypes.html?highlight=str%20count#str.count
Here is how it could be used in your case:
openfile = input('Enter the input file: ') # Gets the input file from the user
word = 'PMID' # The word we are trying to count
try:
with open(openfile, 'r') as f:
# Uses a context manager (the 'with' keyword) so that we do not need to manually close the file
word_count = f.read().count(word)
# Reads the entire file and then uses the built-in count() function to determine how many times the entry appears
print(f'The word {word} appears {word_count} times.')
quit() # Exits the program
except FileNotFoundError:
print('Input file not found.')
print('Please check the file name or the location of your input file.')
I hope this was what you were looking for!

You may try something like:
for line in file:
accumulator += entry in line
# This does not work if the entry occurs more than once on the same line.

The problem is that you are calling exit() inside your for loop. This means that after the first iteration (when accumulator=1), you are ending the loop. Move this instruction out of the loop for this to work properly.
import os
openfile = input('Enter the input file: ')
accumulator = 0
word = "PMID"
if os.path.isfile(openfile):
file = open(openfile,'r')
for entry in file.readlines():
accumulator +=1
print(f'There are {accumulator} occurences of "{word}" in {openfile}')
else:
print('Input file not found.')
print('Please check the file name or the location of your input
If you then want to count the occurences of a specific word...
import os
openfile = input('Enter the input file: ')
accumulator = 0
word = "PMID"
if os.path.isfile(openfile):
file = open(openfile,'r')
for entry in file.readlines():
if word in entry:
accumulator +=1
print(f'There are {accumulator} occurences of "{word}" in {openfile}')
else:
print('Input file not found.')
print('Please check the file name or the location of your input file.')

Related

How can i sort order of wordcount with Python?

I am using this code to count the same words in a text file.
filename = input("Enter name of input file: ")
file = open(filename, "r", encoding="utf8")
wordCounter = {}
with open(filename,'r',encoding="utf8") as fh:
for line in fh:
# Replacing punctuation characters. Making the string to lower.
# The split will spit the line into a list.
word_list = line.replace(',','').replace('\'','').replace('.','').replace("'",'').replace('"','').replace('"','').replace('#','').replace('!','').replace('^','').replace('$','').replace('+','').replace('%','').replace('&','').replace('/','').replace('{','').replace('}','').replace('[','').replace(']','').replace('(','').replace(')','').replace('=','').replace('*','').replace('?','').lower().split()
for word in word_list:
# Adding the word into the wordCounter dictionary.
if word not in wordCounter:
wordCounter[word] = 1
else:
# if the word is already in the dictionary update its count.
wordCounter[word] = wordCounter[word] + 1
print('{:15}{:3}'.format('Word','Count'))
print('-' * 18)
# printing the words and its occurrence.
for word,occurance in wordCounter.items():
print(word,occurance)
I need them to be in order in bigger number to smaller number as output. For example:
word 1: 25
word 2: 12
word 3: 5
.
.
.
I also need to get the input as just ".txt" file. If the user writes anything different the program must get an error as "Write a valid file name".
How can i sort output and make the error code at the same time ?
For printing in order, you can sort them prior to printing by the occurrence like this:
for word,occurance in sorted(wordCounter.items(), key=lambda x: x[1], reverse=True):
print(word,occurance)
In order to check whether the file is valid in the way that you want, you can consider using:
import os
path1 = "path/to/file1.txt"
path2 = "path/to/file2.png"
if not path1.lower().endswith('.txt'):
print("Write a valid file name")
if not os.path.exists(path1):
print("File does not exists!")
You can try:
if ( filename[-4:0] != '.txt'):
print('Please input a valid file name')
And repeat input command...

When searching for variable in txt file, does not look past first row [duplicate]

This question already has answers here:
How to read a file line-by-line into a list?
(28 answers)
Closed 1 year ago.
when searching for a user number in a text file, it looks at only the first line but never goes to the second line. It looks for the last variable in the line as that is what I am using in another file.
I also get this error if line 8 is a 'while' instead of 'if'.
Thanks for any help!
count =0
user_text = str(input("Enter user num"))
found = False
def Check(user_text):
global count, found
#while looking for line
fh =open("user_info.txt", "r")
if found == False:
print("Here")
s =fh.readline(count)
print(s)
#seperate the words
N,M,A,B=s.split("~")
#if its found
if user_text in B:
found = True
print ("line Number:", count, ":", s)
count+=1
print(found)
fh.close()
return found
found = Check(user_text)
You are using fh.readline(). This returns one line from the file, and you can use it with an optional parameter that shows the number of bytes from the line to return. If you want to iterate over the lines, use fh.readlines() (returns a list of lines).

I am struggling with reading specific words and lines from a text file in python

I want my code to be able to find what the user has asked for and print the 5 following lines. For example if the user entered "james" into the system i want it to find that name in the text file and read the 5 lines below it. Is this even possible? All i have found whilst looking through the internet is how to read specific lines.
So, you want to read a .txt file and you want to read, let's say the word James and the 5 lines after it.
Our example text file is as follows:
Hello, this is line one
The word James is on this line
Hopefully, this line will be found later,
and this line,
and so on...
are we at 5 lines yet?
ah, here we are, the 5th line away from the word James
Hopefully, this should not be found
Let's think through what we have to do.
What We Have to Do
Open the text file
Find the line where the word 'James' is
Find the next 5 lines
Save it to a variable
Print it
Solution
Let's just call our text file info.txt. You can call it whatever you want.
To start, we must open the file and save it to a variable:
file = open('info.txt', 'r') # The 'r' allows us to read it
Then, we must save the data from it to another variable, we shall do it as a list:
file_data = file.readlines()
Now, we iterate (loop through) the line with a for loop, we must save the line that 'James' is on to another variable:
index = 'Not set yet'
for x in range(len(file_data)):
if 'James' in file_data[x]:
index = x
break
if index == 'Not set yet':
print('The word "James" is not in the text file.')
As you can see, it iterates through the list, and checks for the word 'James'. If it finds it, it breaks the loop. If the index variable still is equal to what it was originally set as, it obviously has not found the word 'James'.
Next, we should find the five lines next and save it to another variable:
five_lines = [file_data[index]]
for x in range(5):
try:
five_lines.append(file_data[index + x + 1])
except:
print(f'There are not five full lines after the word James. {x + 1} have been recorded.')
break
Finally, we shall print all of these:
for i in five_lines:
print(i, end='')
Done!
Final Code
file = open('info.txt', 'r') # The 'r' allows us to read it
file_data = file.readlines()
index = 'Not set yet'
for x in range(len(file_data)):
if 'James' in file_data[x]:
index = x
break
if index == 'Not set yet':
print('The word "James" is not in the text file.')
five_lines = [file_data[index]]
for x in range(5):
try:
five_lines.append(file_data[index + x + 1])
except:
print(f'There are not five full lines after the word James. {x + 1} have been recorded.')
break
for i in five_lines:
print(i, end='')
I hope that I have been helpful.
Yeah, sure. Say the keyword your searching for ("james") is keywrd and Xlines is the number of lines after a match you want to return
def readTextandSearch1(txtFile, keywrd, Xlines):
with open(txtFile, 'r') as f: #Note, txtFile is a full path & filename
allLines = f.readlines() #Send all the lines into a list
#with automatically closes txt file at exit
temp = [] #Dim it here so you've "something" to return in event of no match
for iLine in range(0, len(allLines)):
if keywrd in allLines[iLine]:
#Found keyword in this line, want the next X lines for returning
maxScan = min(len(allLines),Xlines+1) #Use this to avoid trying to address beyond end of text file.
for iiLine in range(1, maxScan):
temp.append(allLines[iLine+iiLine]
break #On assumption of only one entry of keywrd in the file, can break out of "for iLine" loop
return temp
Then by calling readTextandSearch1() with appropriate parameters, you'll get a list back that you can print at your leisure. I'd take the return as follows:
rtn1 = readTextAndSearch1("C:\\Docs\\Test.txt", "Jimmy", 6)
if rtn1: #This checks was Jimmy within Test.txt
#Jimmy was in Test.txt
print(rtn1)

Check if random number already exists

i would like to generate random numbers and write them to a .txt file.
The range is : str(random.randint(0,10))
Before it generates me a random number my code should first check the .txt file. In this text file are already written down some random numbers. If the random number already exists it should generate me a new one and add it to my .txt file.
randomTxt = './random.txt'
def checkRandomNumberExists(value):
with open(randomTxt, 'a+') as random:
genRandom = str(random.randint(1,10))
if value in random:
random.write(genRandom)
random.write('\n')
I quess i am on the wrong way.
Can anyone please help me.
Thank you in advance
I don't see any reason to use argument in function because you're generating random number and you are generating random number between 1-10, what if all number is added in the text should it add number other than 1,2,3...10, please edit you question and mention that.
Below code will check if number is present in the text file or not, it will add the number in text file if it's not present else it will print the message that number already exits.
Code
import random
lines = []
num = random.randint(0,10)
f= open("random.txt","w+")
def checkRandomNumberExists():
with open("random.txt") as file:
for line in file:
line = line.strip()
lines.append(line)
if str(num) not in lines:
with open("random.txt", "a") as myfile:
myfile.write(str(num)+'\n')
print(str(num)+ ' is added in text file')
else:
print(str(num)+ ' is already exists in text file')
checkRandomNumberExists()
Output when inserted new value
7 is added in text file
Output when value is already in text file
7 is already exists in text file
Try using a while loop inside:
randomTxt = './random.txt'
with open(randomTxt, 'a+') as file:
text = file.read()
genRandom = str(random.randint(1,10))
while genRandom in text:
genRandom = str(random.randint(1,10))
file.write(genRandom)
file.write('\n')
Note: Please do not name files and variables a builtin name (i.e random) because it might override the original module.

Find the line number a string is on in an external text file

I am trying to create a program where it gets input from a string entered by the user and searches for that string in a text file and prints out the line number. If the string is not in the text file, it will print that out. How would I do this? Also I am not sure if even the for loop that I have so far would work for this so any suggestions / help would be great :).
What I have so far:
file = open('test.txt', 'r')
string = input("Enter string to search")
for string in file:
print("") #print the line number
You can implement this algorithm:
Initialize a counter
Read lines one by one
If the line matches the target, return the current count
Increment the count
If reached the end without returning, the line is not in the file
For example:
def find_line(path, target):
with open(path) as fh:
count = 1
for line in fh:
if line.strip() == target:
return count
count += 1
return 0
A text file differs from memory used in programs (such as dictionaries and arrays) in the manner that it is sequential. Much like the old tapes used for storage a long, long time ago, there's no way to grab/find a specific line without combing through all prior lines (or somehow guessing the exact memory location). Your best option is just to create a for loop that iterates through each line until it finds the one it's looking for, returning the amount of lines traversed until that point.
file = open('test.txt', 'r')
string = input("Enter string to search")
lineCount = 0
for line in file:
lineCount += 1
if string == line.rstrip(): # remove trailing newline
print(lineCount)
break
filepath = 'test.txt'
substring = "aaa"
with open(filepath) as fp:
line = fp.readline()
cnt = 1
flag = False
while line:
if substring in line:
print("string found in line {}".format(cnt))
flag = True
break
line = fp.readline()
cnt += 1
if not flag:
print("string not found in file")
If the string will match a line exactly, we can do this in one-line:
print(open('test.txt').read().split("\n").index(input("Enter string to search")))
Well the above kind of works accept it won't print "no match" if there isn't one. For that, we can just add a little try:
try:
print(open('test.txt').read().split("\n").index(input("Enter string to search")))
except ValueError:
print("no match")
Otherwise, if the string is just somewhere in one of the lines, we can do:
string = input("Enter string to search")
for i, l in enumerate(open('test.txt').read().split("\n")):
if string in l:
print("Line number", i)
break
else:
print("no match")

Categories