I have created a lambda that will scan an uploaded file and search for specific phrases which have been listed in another s3 bucket. If a phrase is matched in the original uploaded file, it will print the line of the transcript as well as the response.
This lambda works if we upload each transcript individually, however if we upload more than 1, it stores the original output and adds it to the beginning.
I feel that this issue may be caused by the /tmp/ file not being cleared when the lambda function ends.
Is there a way to clear the /tmp/ file each time a job is done?
The output looks as follows:
ch_0 : Okay. And then, um, how do you guys typically allocate funding for a project like this?
-------------------------------------------------------------
ch_1 : Yeah, we do have capital projects and we've allocated money 3 place, which is and stuff, Um, every year.
ch_0 : Okay. And then, um, how do you guys typically allocate funding for a project like this?
-------------------------------------------------------------
ch_1 : Yeah, we do have capital projects and we've allocated money 3 place, which is and stuff, Um, every year.
ch_0 : Okay. And then, um, how do you guys typically allocate funding for a project like this?
-------------------------------------------------------------
ch_1 : Yeah, we do have capital projects and we've allocated money 3 place, which is and stuff, Um, every year.
However, it should look like this:
ch_0 : Okay. And then, um, how do you guys typically allocate funding for a project like this?
-------------------------------------------------------------
ch_1 : Yeah, we do have capital projects and we've allocated money 3 place, which is and stuff, Um, every year.
My lambda code is as follows:
import boto3
def lambda_handler(event, context):
s3 = boto3.client("s3")
if event:
file_obj = event["Records"][0]
bucketname = str(file_obj['s3']['bucket']['name'])
filename = str(file_obj['s3']['object']['key'])
job_name = filename
print("Filename: ", filename)
fileObj = s3.get_object(Bucket=bucketname, Key=filename)
file_content = fileObj["Body"].read().decode('utf-8')
budget_file = s3.get_object(Bucket= "bantp-phrases", Key="B.txt")
budget_content = budget_file["Body"].read().decode('utf-8')
authority_file = s3.get_object(Bucket= "bantp-phrases", Key="A.txt")
authority_content = authority_file["Body"].read().decode('utf-8')
need_file = s3.get_object(Bucket= "bantp-phrases", Key="N.txt")
need_content = need_file["Body"].read().decode('utf-8')
timeline_file = s3.get_object(Bucket= "bantp-phrases", Key="T.txt")
timeline_content = timeline_file["Body"].read().decode('utf-8')
partner_file = s3.get_object(Bucket= "bantp-phrases", Key="P.txt")
partner_content = partner_file["Body"].read().decode('utf-8')
# Converts all to a list
budgets = budget_content.split("\n")
authorities = authority_content.split("\n")
needs = need_content.split("\n")
timelines = timeline_content.split("\n")
partners = partner_content.split("\n")
lines = file_content.split("\n")
directory_name = filename
mylist = lines
#Budget Phrase Analysis
for b in budgets:
with open("/tmp/budget.txt", "a") as x:
try:
output = None
for index, line in enumerate(lines):
if b.strip() in line:
output = index
break
if output:
x.write("\n" + lines[output] + "\n")
x.write("-------------------------------------------------------------")
x.write("\n" + lines[output +1] + "\n")
print ("It worked!")
break
except (ValueError):
x.write("Nothing found")
print ("It didn't work :(")
break
s3.upload_file(Filename = "/tmp/budget.txt" , Bucket="bantp-analysis", Key = ((directory_name)+'/'+"Budget_" + (filename)))
#Authority Phrase Analysis
for a in authorities:
with open("/tmp/authority.txt", "a") as c:
try:
output = None
for index, line in enumerate(lines):
if a.strip() in line:
output = index
if output:
c.write("\n" + lines[output] + "\n")
c.write("-------------------------------------------------------------")
c.write("\n" + lines[output +1] + "\n")
print ("It worked!")
except (ValueError):
c.write("Nothing found")
print ("It didn't work :(")
s3.upload_file(Filename = "/tmp/authority.txt" , Bucket="bantp-analysis", Key = ((directory_name)+'/'+"Authority_") + (filename))
#Need Phrase Analysis
for n in needs:
with open("/tmp/need.txt", "a") as v:
try:
output = None
for index, line in enumerate(lines):
if n.strip() in line:
output = index
break
if output:
v.write("\n" + lines[output] + "\n")
v.write("-------------------------------------------------------------")
v.write("\n" + lines[output +1] + "\n")
print ("It worked!")
break
except (ValueError):
v.write("Nothing found")
print ("It didn't work :(")
break
s3.upload_file(Filename = "/tmp/need.txt" , Bucket="bantp-analysis", Key = ((directory_name)+'/'+"Need_") + (filename))
#Timeline Phrase Analysis
for t in timelines:
with open("/tmp/timeline.txt", "a") as z:
try:
output = None
for index, line in enumerate(lines):
if t.strip() in line:
output = index
break
if output:
z.write("\n" + lines[output] + "\n")
z.write("-------------------------------------------------------------")
z.write("\n" + lines[output +1] + "\n")
print ("It worked!")
break
except (ValueError):
z.write("Nothing found")
print ("It didn't work :(")
break
s3.upload_file(Filename = "/tmp/timeline.txt" , Bucket="bantp-analysis", Key = ((directory_name)+'/'+"Timeline_") + (filename))
#Partner Phrase Analysis
for p in partners:
with open("/tmp/partner.txt", "a") as q:
try:
output = None
for index, line in enumerate(lines):
if p.strip() in line:
output = index
break
if output:
q.write("\n" + lines[output] + "\n")
q.write("-------------------------------------------------------------")
q.write("\n" + lines[output +1] + "\n")
print ("It worked!")
except (ValueError):
q.write("Nothing found")
print ("It didn't work :(")
s3.upload_file(Filename = "/tmp/partner.txt" , Bucket="bantp-analysis", Key = ((directory_name)+'/'+"Partner_") + (filename))
Welcome to stackoverflow!
Can you try the following solutions and comment the results please
In all your open operation change the mode of opening the file from a to w.
Example
with open("/tmp/timeline.txt", "a") as z:
to
with open("/tmp/timeline.txt", "w") as z:
This change for all open operations, to override the existing metafile. Also do take care of indentation.
Related
#This program takes in a text file and whatever the ser types in ; it searches for the specific word or phrase and then print out in which line this word or phrase is located .
If i feed it a text file with 20 lines , it produces normal results
As soon as i give it a 3000 worded document it produces error
Can anyone explain this
while True:
search = str(input("==>"))
line_number = 1
fslope = open("searching_in_a_textfile")
for line in fslope:
if search.lower() in line:
print("tHE LINE NUMBER IS ", line_number)
print("THE LINE SAYS : " + line)
line_number = line_number + 1
continue
if search.upper() in line:
print("tHE LINE NUMBER IS ", line_number)
print("THE LINE SAYS : " + line)
line_number = line_number + 1
continue
if search.title() in line:
print("tHE LINE NUMBER IS ", line_number)
print("THE LINE SAYS : " + line)
line_number = line_number + 1
continue
else:
line_number = line_number + 1
continue
print("END OF PRIOCESS")
First lets make it simple: (this code is almost the same as yours)
lines = []
with open('searching_in_a_textfile') as f:
lines = f.readlines()
while True:
search = input('==>')
if not search:
break
for line_number, line in enumerate(lines, 1):
if search.lower() in line.lower():
print('tHE LINE NUMBER IS ', line_number, '\nTHE LINE SAYS :', line)
print("END OF PRIOCESS")
Now when the input is '' (empty string / no input) the process will stop.
if you can add your error it could be very helpful.
The number of "*" printed fluctuates according to the length of string entered
def main():
# check command line argument
if len(argv) != 2:
print("Usage: python bleep.py dictionary")
exit(1)
else:
ban = set()
# Load txt file
with open(argv[1], "r") as f:
for line in f:
# strip the space and add to set
ban.add(line.strip())
# prompt user input
input = get_string("What message would you like to censor?\n")
# Split input into word tokens
token = input.split()
censored_msg = ""
for i in token:
if i.lower() in ban:
censored_msg = (censored_msg + "*"*(len(token)+1) + " ")
else:
censored_msg += i + " "
# print censored message
print(censored_msg.strip())
if __name__ == "__main__":
main()
It prints fine for some cases, such as
Input: heck my gosh
Output: **** my ****
But not so in others (should be **** ****)
Input: heck gosh
Output: * * (just 6 * for 8 letters)
Is it a typo? Check this line very carefully
censored_msg = (censored_msg + "*"*(len(token)+1) + " ")
and remember which is what here for i in token:
I am a noob in python and i need help.I have made a phonebook where you can add the contacts.But the problem is that when i exit the program the changes to the list are not saved.I want the user to be able to make permanent changes to the list.I have seen posts about a file=open("something",'w') code to do this(I think) but i dont know where to insert this code and i dont really understand what it is.Could someone help me understand what this is about..Here is the full code:
name = ["ranga","hari"]
number = [9895497777,9]
book = {name[0]:number[0],name[1]:number[1]}
def search():
print("Contacts:")
for x in book:
print(x,':',book[x])
while 1:
count = 0
a = 0
ch1 = input("search: ")
try:
ch1 = int(ch1)
except ValueError:
while a < len(name):
result = name[a].find(ch1)
if result == -1:
a = a + 1
else:
print(name[a],number[a])
a = a + 1
count = count + 1
if count == 0:
print("Not available.Try again")
continue
else:
break
ch1 = str(ch1)
while a < len(number):
sumber = str(number[a])
result = sumber.find(ch1)
if result == -1:
a = a + 1
else:
print(name[a],number[a])
a = a + 1
count += 1
if count == 0:
print("Not available.try again")
continue
else:
break
def add():
print("What is the name of the contact you want to add?")
name1 = input()
name.append(name1)
while 1:
print("What is the number of this contact?")
number1 = input()
try:
number1 = int(number1)
except ValueError:
print("Please type a number..")
continue
number.append(number1)
book[name1] = number1
break
def remoe():
print("Reference:")
for x in book:
print(x,':',book[x])
while 1:
print("What is the name of the contact you want to remove?")
name2 = input()
if name2 in book:
increment = name.index(name2)
name.pop(increment)
number.pop(increment)
del book[name2]
break
else:
print("Not available.Please try again")
while 1:
print("Contacts:")
for x in book:
print(x, ':', book[x])
print("\nWhat do you want to do?\n1.Search for a person\n2.edit the phone book\n3.exit")
choice = input()
try:
choice = int(choice)
except ValueError:
print("Type 1,2 or 3")
continue
if choice == 1:
search()
elif choice == 2:
while 1:
print("Do you want to:\n1.Add a contact\n2.Remove a contact\n3.Go back to main menu")
ch2 = input()
if ch2 in['3']:
break
else:
try:
ch2 = int(ch2)
except ValueError:
print("Type 1 or 2..")
if ch2 == 1:
add()
elif ch2 == 2:
remoe()
elif choice == 3:
exit()
else:
print("Type 1,2 or 3")
I appreciate the help.
When you choose to add a contact, it does properly add the name and number to the list. But, that is it.
When you re-run the program, the list gets re-assigned due to the first 2 lines of your code:
name = ["ranga","hari"]
number = [9895497777,9]
So, you won't see the last changes.
This is where you should maintain a file which lives outside the scope of your code, rather than a list.
You can modify your add function like this:
def add():
print("What is the name of the contact you want to add?")
name1 = input()
#name.append(name1)
# Just add the name1 variable's value to the file
with open('contacts_list.txt', 'a+') as f:
f.write(name1 + '\n')
while 1:
print("What is the number of this contact?")
number1 = input()
try:
number1 = int(number1)
except ValueError:
print("Please type a number..")
continue
#number.append(number1)
# Similarly, append the number1 variable's value to file again.
with open('contacts_list.txt', 'w+') as f:
f.write(number1)
#book[name1] = number1
with open('contacts_list.txt', 'r') as f:
print(f.read())
break
Note: You would also need to change the other functions search and remove to read and write from the file. I've just given you a taste of how things are done. You need to modify your code and make it work.
Let me know if it helps.
I took your advice and made a new text file but i still did not know how to do it but after reading ur answers i understood and at last i came to this..
removelist = []
def search():
while 1:
search = str(input("Search: "))
if search not in["exit", "Exit"]:
with open('output.txt', 'r+') as f:
line = f.readline()
while line:
data = line.find(search)
if not data == -1:
print(line.rstrip('\n'))
line = f.readline()
else:
line = f.readline()
else:
break
f.close()
def add():
print("Type the name of the contact:")
name = input()
while 1:
print("Type the number of this contact:")
number = input()
try:
number = int(number)
except ValueError:
print("Please type a number")
continue
number = str(number)
with open('output.txt', 'a+') as f:
f.write('\n' + name +' ' + number)
break
def remoe(): #this is where the problem comes in
while 1:
remove = str(input("Remove: "))
with open('output.txt', 'r+') as f:
line = f.readline()
while line:
if not remove in["Remove", "remove"]:
removelist.clear()
data = line.find(remove)
if not data == -1:
removelist.append(line) #This saves all the lines coming from the search to a
print(removelist) #removelist which can be accessed when you type in remove
line = f.readline() #But the problem is that if there is a \n at the end of the
else: #string then the remove function does not work
line = f.readline()
else:
print(removelist)
with open('output.txt', 'r') as f:
d = f.readlines()
f.close()
with open('output.txt', 'w') as f:
for i in d:
if i not in removelist:
f.write(i)
f.truncate()
f.close()
break
while 1:
with open('output.txt', 'r') as f:
data = f.read()
print("Contacts:")
print(data)
print('''What do you want to do?
1.Search for a contact
2.Edit contacts
3.Exit''')
f.close()
choice = input()
if choice in["1"]:
search()
elif choice in["2"]:
while 1:
print('''What do you wanna do:
1.Add a contact
2.Remove a contact
3.Exit to main menu''')
ch1 = input()
if ch1 in["1"]:
add()
elif ch1 in["2"]:
remoe()
elif ch1 in["3"]:
break
else:
print("Please type 1,2 or 3")
elif choice in[3]:
print("Ok bye")
else:
print("Please type 1,2 or 3")
Now the problem seems to be the remove function..if i try to remove a line with \n at the end of it then it wont work while the opp. seems to work.Any guess what i am doing here?
And thanks for the help Mayank porwal
At the first you should know name = ["ranga","hari"], number = [9895497777,9] that you have defined are in the code and you can not change those value, and after exit() they will reset to default value.
you should use of file (for example .txt file) in this issue:
1. you must create a .txt file in your project (for example Contacts.txt)
2. and write your information in there (for example in first line: Kourosh +98938....)
3. at the first step in your program you must read Contact.txt and load it in a structure like a list or dictionary (for example
>>> with open('workfile') as f:
... read_data = f.read()
>>> f.closed
)
4.now you can edit, add, remove structure.
5.and finally you can write structure in the file, before exit()
for example:
>>> with open('workfile') as f:
... f.write(s)
>>> f.closed
This code is for a trivia game, and every time someone gets a question right, they are awarded with a certain amount of points per question. I do not know how to get the code to add the points together after each right answer. Every time I try something different, I always get this error message.
import sys
def open_file(file_name, mode):
"""Open a file."""
try:
the_file = open(file_name, mode)
except IOError as e:
print("Unable to open the file", file_name, "Ending program.\n", e)
input("\n\nPress the enter key to exit.")
sys.exit()
else:
return the_file
def next_line(the_file):
"""Return next line from the trivia file, formatted."""
line = the_file.readline()
line = line.replace("/", "\n")
return line
def next_block(the_file):
"""Return the next block of data from the trivia file."""
category = next_line(the_file)
question = next_line(the_file)
answers = []
for i in range(4):
answers.append(next_line(the_file))
correct = next_line(the_file)
if correct:
correct = correct[0]
explanation = next_line(the_file)
points = next_line(the_file)
return category, question, answers, correct, explanation, points
def welcome(title):
"""Welcome the player and get his/her name."""
print("\t\tWelcome to Trivia Challenge!\n")
print("\t\t", title, "\n")
def main():
trivia_file = open_file("trivia_points.txt", "r")
title = next_line(trivia_file)
welcome(title)
score = 0
# get first block
category, question, answers, correct, explanation, points = next_block(trivia_file)
while category:
# ask a question
print(category)
print(question)
for i in range(4):
print("\t", i + 1, "-", answers[i])
# get answer
answer = input("What's your answer?: ")
# check answer
if answer == correct:
print("\nRight!", end=" ")
total = sum(points + points)
score = total
else:
print("\nWrong.", end=" ")
print(explanation)
print("Score:", score, "\n\n")
points = int(points)
# get next block
category, question, answers, correct, explanation, points = next_block(trivia_file)
trivia_file.close()
print("That was the last question!")
print("You're final score is", score)
main()
input("\n\nPress the enter key to exit.")
You're getting the points out as a string, and need to convert them to an int before doing any computation with it, ideally as soon as possible:
points = int(next_line(the_file))
in next_block should do the trick.
Also, you are not adding the points to the score, you're replacing it.
total = sum(points + points)
score = total
should be
score += points
to add 'points' to 'score'.
I don't know where does it goes wrong. I can get the correct result if I just call out my valid_ISBN(isbn) function, but when I write the file, the result become all invalid. (maybe something wrong with function call, but I don't know how to fix it)
def main():
# Call and open the File
inFile = open("isbn.txt", "r")
for line in inFile:
line_strip = line.replace("-", "").replace(" ", "").rstrip("\n")
isbn = line_strip # the function call
# Output file
str = []
str.append(line)
outFile = open("isbnOut.txt", "a")
for i in str:
if valid_ISBN(isbn) == "valid":
outFile.write(i.strip() + " valid\n")
else:
outFile.write(i.strip() + " invalid\n")
inFile.close()
outFile.close()
def valid_ISBN(isbn):
if len(isbn) != 10 or (isbn[0:9].isdigit()) == False:
print("invalid")
else:
return partial_sums(isbn)
def partial_sums(s1):
lst1 =[]
sum1 = 0
for i in range(len(s1)):
if (i == (len(s1) -1)) and ((s1[i] == "x") or (s1[i] == "X")):
sum1 = sum1 + 10
else:
sum1 = sum1 + int(s1[i])
lst1.append(sum1)
#print(lst1)
sum_of_s1(lst1)
def sum_of_s1(s2):
lst2 = []
sum2 = 0
for i in s2:
sum2 += i
lst2.append(sum2)
#print(lst2)
checkISBN(lst2[-1])
def checkISBN(value):
if value % 11 == 0:
print("valid")
else:
print("invalid")
main()
2 Test case for isbn text file (no new line):
019-923-3241
818-851-703X
In your main function:
Every time you read a line in from your input file you initialize str and fill it with just one value. You open your output file, do your validity checks for your one value in str, and finally write the one value to the the output file.
The next time you read the file you do the same stuff... so str isn't needed at all
Also using str as a variable name is bad form. In your console write in help(str) and you will see why.
Now to deal with your actual complaint:
Your problem is the fact that there is no new line.
when you say for line in some_open_file_handler:... what python does is populate line with everything up to the next newline character or the end of the file.
If your input file has no new lines seperating isbns then the first value of line would be 019-923-3241 818-851-703X. Thus the line line_strip = line.replace("-", "").replace(" ", "").rstrip("\n") set linestrip to 0199233241818851703X
This should fix it:
'line.split() will yield ['019-923-3241','818-851-703X']
outFile = open("isbnOut.txt", "a")
for line in open("isbn.txt", "r"):
isbns = line.split() #this splits stuff up by whitespace.
for isbn in isbns:
isbn = isbn.replace("-", "").replace(" ", "").rstrip("\n")
if valid_ISBN(isbn) == "valid":
outFile.write(i.strip() + " valid\n")
else:
outFile.write(i.strip() + " invalid\n")