So I want to make this script look for a word, which you can determine, and its line number. I want it to print out the text in this line and the two following ones. How do I code the last part (print the next three lines)?
def search_passwords():
file = open("D:\\Libaries\\Documents\\resources_py\\pw.txt", "r")
print()
search = input("Enter service: ")
print()
for line in file:
if search in line:
print(the next three lines)
There are some corner cases that you did not mention.
What if multiple lines have the searching word? What is your expected behavior?
What if the line right after the matching line has the searching word?
How large is the file and could you load it in memory?
If there's only one matching, or there won't be any matching in the lines you print right after, then you can use a single counter
def search_passwords():
file = open("D:\\Libaries\\Documents\\resources_py\\pw.txt", "r")
print()
search = input("Enter service: ")
print()
counter = 0
for line in file:
if counter > 0:
counter -= 1
print(line)
if search in line:
counter = 2
print(line)
However, this will double print the lines if the following lines containing the searching word. You can of course make the second if elif, but then it will just ignore the following matching pattern.
And, if you only need the first or only appearance, you should break out of the loop once you print everything, not read the whole file.
Just like I said, you need to know the expected behavior and the requirement better, to write a solid program.
There could be a better solution but this is a bit more flexible with a nigher amount of following rows.
def search_passwords():
file = open("D:\\Libaries\\Documents\\resources_py\\pw.txt", "r")
print()
search = input("Enter service: ")
print()
counter = 3
found = False
for line in file:
if search in line:
found = True
if found:
if counter > 0:
counter -= 1
print(line, counter)
if counter == 0:
break
Yet another approach with not found error handling:
with open('file.txt', 'r') as f:
lines = [line.strip() for line in f.readlines()]
try:
i = lines.index(input('Enter service: '))
for j in range(i, i+3):
try:
print(lines[j])
except IndexError:
pass
except ValueError:
print('Service not found!')
Related
I have a file called 'dictionary.txt' which contains words of the dictionary in alphabetical order.
I want to search if certain words are in this dictionary.
Could anyone help me write code that could open this file, then I write a word as an input and I receive an output that says if the word is in the dictionary file or not?
Here's what I have so far:-
dicfile = open('dictionary.txt', 'r')
word = input('enter a word: ')
if word in dicfile:
print('true')
else:
print('false')
fd = open("abc.txt","r") # open the file in read mode
file_contents = fd.read() # read file contents
word = "hello" # input word to be searched
if(word in file_contents): # check if word is present or not
print("word found")
else:
print("word not found")
fd.close() # close the file
You were almost there, just needed a little more info about file handling in python.
The dictionary.txt file:
bac def ghij kl mano pqrstuv
Here's the your modified code code
dicfile = open('dictionary.txt', 'r')
file1 = dicfile.read()
file1 = file1.split()
word = input('enter a word: ')
if word in file1:
print('true')
else:
print('false')
The Output:
Test Case 1
$ python3 test_9.py
enter a word: bac
true
Test Case 2
$ python3 test_9.py
enter a word: anyword
false
def check_word(filename, word):
with open(filename) as fin:
if word in fin.read():
return True
else:
return False
Suppose you want to find x in the file. You can do like this :
with open("textfile.txt") as openfile:
for line in openfile:
for part in line.split():
if "x" in part:
print part
else:
print false
`
This is one method using procedural programming
dictionary = open('dictionary.txt', 'r') #Only read
word = input('Enter a word:\n) #input typed on line below this
if word in dictionary:
print('{} is in the dictionary (True)'.format(word))
else:
print('{} is not in the dictionary (False)'.format(word))
Here's another one:
def in_file(filename, word):
file = open(filename, 'r')
if word in file:
return True
else:
return False
word = input("Enter a word:\n")
in_file('dictionary.txt', word)
For non-case sensitive results you could use word.lower(). Hope this helps.
Stack Overflow is not code writing service, We try to help people only when they try to do something and stuck , I am not giving an exact solution but I will show you how you can achieve your goal, and rest is your homework:
Good answer by #Srikar Appalaraju
#Sample 1 - elucidating each step but not memory efficient
lines = []
with open("C:\name\MyDocuments\numbers") as file:
for line in file:
line = line.strip() #or some other preprocessing
lines.append(line) #storing everything in memory!
#Sample 2 - a more pythonic and idiomatic way but still not memory efficient
with open("C:\name\MyDocuments\numbers") as file:
lines = [line.strip() for line in file]
#Sample 3 - a more pythonic way with efficient memory usage. Proper usage of with and file iterators.
with open("C:\name\MyDocuments\numbers") as file:
for line in file:
line = line.strip() #preprocess line
doSomethingWithThisLine(line) #take action on line instead of storing in a list. more memory efficient at the cost of execution speed.
Example :
with open('dictionary.txt','r') as f:
#for reading line by line
for line in f:
print(line)
#reading whole file once
print(f.read())
Now how to check word in file :
Using python 'in' operator
#checking word
if 'some_word' in line:
print(True)
else:
print(False)
Now try something and when you stuck then ask help, instead of asking to write code.
import random
com=input("")
if com.startswith("/tip"):
numlines=sum(1 for line in open("C:\\Users\\Jace\\Desktop\\Python Programs\\Quote\\tip.txt"))-1
randomint=random.randint(0, numlines)
with open("C:\\Users\\Jace\\Desktop\\Python Programs\\Quote\\tip.txt", "r") as f:
i=1
for line in f:
if i==randomint:
break
i+=1
print(line.strip("\n"))
This is the part of the code for my random tips from a file so far. I wish to add another part of code where it adds all strings with any occurrence of the input placed after "/tip ", for example, if I were to type "/tip Hello", it would compile all lines in the text file with "Hello" in the string and do a random.choice() from the list, printing the one chosen. I don't really know where to start with this, any help would be appreciated. Thanks in advance!
You don't have to store all of the lines in a list. You can read the lines, selecting one at random and discarding the rest. This is called "resevoir sampling".
Your code might look like this:
import random
def random_line(iterator):
result = None
for n, item in enumerate(iterator):
if random.randint(0,n) == 0:
result = item
return result
# A random line
with open('tip.txt') as f:
print random_line(f) or "No tip for you!"
# A random line that has 'Hello'
with open('tip.txt') as f:
print random_line(line for line in f if 'Hello' in line) or "nothin!"
As a more special case, this code randomly chooses a matching line from the tips file, but falls back to a random non-matching line if no match exists. It has the advantages of reading the input file exactly once, and not having to store the entire tips file in memory.
import random
def random_line_with_fallback(iterator, match = lambda x: True):
result_match = None
result_all = None
n_match = n_all = 0
for item in iterator:
if match(item):
if random.randint(0, n_match) == 0:
result_match = item
n_match += 1
if random.randint(0, n_all) == 0:
result_all = item
n_all += 1
return (result_match or result_all).strip()
# A random line
with open('tip.txt') as f:
print random_line_with_fallback(f)
# Another way to do a random line. This depends upon
# the Python feature that "'' in line" will always be True.
com = ''
with open('tip.txt') as f:
print random_line_with_fallback(f, lambda line: com in line)
# A random line that has 'Hello', if possible
com = 'Hello'
with open('tip.txt') as f:
print random_line_with_fallback(f, lambda line: com in line)
References:
https://stackoverflow.com/a/23840292/8747
https://en.wikipedia.org/wiki/Reservoir_sampling
I think this is what you want, process each line of a text file, checking if the line has the word you're looking for. If so, add it to a list, and the randomly select one "line" for all possible "lines".
lines = []
with open("tip.txt", "r") as f:
for line in f:
if com in line:
lines.append(line)
print(random.choice(lines))
I am trying to print a specific line from the file "Scores", which is option B. This is my code:
print("Option A: Show all scores\nOption B: Show a record\nOption Q: Quit")
decision = input("Enter A, B, C or Q: ")
myFile = open("Scores.txt", "rt")
if decision == "A":
record = myFile.read()
print(record)
myFile.close()
elif decision == "B" or decision == "b":
playerName = input("Enter a player name to view their scores: ")
record = myFile.read()
answer = record.find(playerName)
for line in answer:
print(line)
elif decision == "Q" or decision == "q":
exit
I went for Option B, then I entered a player name that holds the score of the player, but it throws this error message:
line 12, in <module>
for line in answer():
TypeError: 'int' object is not callable
A few cents from my side :
file = open("file")
lines = file.readlines()
for line in lines:
if playername in line:
print line
file.close()
Hope it works!
find() method returns a positive index if it succeeds, -1 otherwise
You should loop on your content line by line, as follows:
for line in myFile:
if line.find(playerName):
print(line)
A safer way to read the file and find data, so that you will not have OutOfMemory issues when storing the whole file in memory.
playerName = input("Enter a player name to view their scores: ")
with open("Scores.txt", 'r') as f:
for row in f:
if playerName in row:
print row
This way you will be using with that will close the file by itself either when the program ends or Garbage Collection kicks in. This way python will read the file line by line and store only 1 line in memory. So you can use huge files and do not worry about memory issues.
Hope it helps :)
Working with str methods will take more acrobatics. Try the following,
import re
p = re.compile(r"\b{}\b".format(playername)) # keep it ready
# inside option B
for line in myfile: # no need to `.read()` it
match = p.search(line)
if match:
print(line)
break # if there is only one record for playername
See if it works for you.
similar thing here:
Reading specific lines only (Python)
fp = open("file")
for i, line in enumerate(fp):
if line == playername:
print line
fp.close()
I also notice you don't close your file for each decision, should make that happen.
Few python idioms and small optimization
Here are many answer, my sample brings in few python idioms and optimize it a bit:
fname = "Scores.txt"
player_name = "Quido"
with open(fname) as f:
for line in f:
if player_name in line:
print line
break
print "Going on doing following tasks."
The with block will close the open file on exiting the inner block. No need to f.close(), safe
in case of problems to read the file.
for line in f: shows, that iterating over file open in text mode we get one line per iteration.
break after we print the line with the player will effectively stop iterating over lines assuming,
there is only one such line or that we are happy with the very first one. If this is not the case,
removing the break allows printing all lines containing the player name.
As lines returned from text file iterator contain new line, you may prefer to get rid of them. Use
print line.strip() in such case what will remove all blank characters from start and end of the line.
Final print is proving, the program continues after it processes all the lines.
It may happen, that you get no output for name, which appears to be present in the file. In such a
case, you might need to clarify letter case. For example, if your text file contains all the names
in exact casing, you have to enter the name properly.
Other option is to lower-case the player_name and compare it against lower cased line:
fname = "Scores.txt"
player_name = "Quido"
normalized_player_name = player_name.lower()
with open(fname) as f:
for line in f:
if normalized_player_name in line.lower():
print line.strip()
break # comment out to print all lines with the player
print "Going on doing following tasks."
Note, that we normalize the player_name outside from the loop to be a bit faster. Lower-casing inside the
loop would work, but would do the same task repeatedly.
The line is printed using exact letter cases as in the file.
I'm trying to make a program that takes a letter the user inputs, reads a text file and then prints the words that start with that letter.
item = "file_name"
letter = raw_input("Words starting with: ")
letter = letter.lower()
found = 0
with open(item) as f:
filelength = sum(1 for line in f)
for i in range (filelength):
word = f.readline()
print word
if word[0] == letter:
print word
found += 1
print "words found:", found
I keep receiving the error
"if word[0] == letter: IndexError: string index out of range"
with no lines being printed. I think this is what happens if there's nothing there, but there are 50 lines of random words in the file, so I'm not sure why it is being read this way.
You have two problems:
You are trying to read the whole file twice (once to determine the filelength, then again to get the lines themselves), which won't work; and
You aren't dealing with empty lines, so if any are introduced (e.g. if the last line is blank) your code will break anyway.
The easiest way to do this is:
found = 0
with open(item) as f:
for line in f: # iterate over lines directly
if line and line[0] == letter: # skip blank lines and any that don't match
found += 1
print "words found:", found
if line skips blanks because empty sequences are false-y, and the "lazy evaluation" of and means that line[0] will only be tried where the line isn't empty. You could alternatively use line.startswith(letter).
When you use sum(1 for line in f) you are already consuming all the lines in your file, so now your handle points to the end of the file. Try using f.seek(0) to return the read cursor to the start of the file.
I am trying to make a program which grabs a list of numbers from a file (which could change in lines and size), and then print out the total of all the numbers and the average. I had no problems doing this when I had a set number of linereads, but am confused on the 'proper' way when the lineread changes every run.
This is my work-in-progress code. I read around a bit and found the correct (?) way of looping through the file to find the length, but not sure how to implement it since it throws some type of IO error currently. Thanks for the help!
def main():
filename = input("Enter file name (name.txt):")
try:
file = open(filename, "r")
except IOError:
print("Error opening file!")
totalLines = totalLineGet(filename)
results = []
for x in range(totalLines):
results.append(getLineNumber(x+1, file))
print("Total = ", numTotal)
print("Average = ", numAvg)
def totalLineGet(_filename):
count = 0
_file = open(_filename, "r")
for x in open(_file):
count+= 1
return count
def getLineNumber(linetoget, _file):
try:
intNumber = int(number = _file.readline())
except ValueError:
print("Error in file data!")
return intNumber
main()
I'm not sure what you want to do... but you should be able to get the answer in one pass.
You can use enumerate() to number an iterable object, in this case a file, if you need to know the item/line number count.
Assuming a single int() per line:
with open(filename, "r") as in_f:
numbers = []
for line in in_f:
line = line.strip() # remove whitespace
if line: # make sure there is something there
number_on_line = int(line)
numbers.append(number_on_line)
sum_of_numbers = sum(numbers)
avg_of_numbers = sum(numbers)/len(numbers)
if this is CSV data you should look into using the csv module, it will split the line into rows/columns for you.
import csv
filename = "somefile"
with open(filename, "rb") as in_f: # <-- notice "rb" is used
reader = csv.reader(in_f)
for row in reader:
for col in row:
# do stuff
...
A simple solution, doing what you want...
filename = 'tmp.txt'
f = open(filename)
s, totnum = 0, 0
for line_number, line in enumerate(f):
nums = map(int, line.split())
s += sum(nums)
totnum += len(nums)
print "numbers:", totnum, "average:", 1.0*s/totnum
This assumes your file only has numbers on each line and not characters, otherwise you'll get a TypeError.
list_of_numbers = []
with open('somefile.txt') as f:
for line in f:
if line.strip(): # this skips blank lines
list_of_numbers.append(int(line.strip()))
print 'Total ',len(list_of_numbers)
print 'Average ',1.0*sum(list_of_numbers)/len(list_of_numbers)
There are some good answers regarding how to do what you want. As for the IO error, the input() built-in attempts to evaluate the user's input which is both dangerous and not what you want.
Try using the raw_input() built-in. That returns the user's input as a string. For fun, try running your script and giving it __name__ as the filename and see what happens.