import re
class WordStatistic:
def __init__(self, keyword, averageScore = 0, occurences = 0):
self.keyword = keyword
self.averageScore = averageScore
self.occurences = occurences
def getWord(self) :
return self.keyword
def getAverageScore(self) :
return self.averageScore
def getOccurences(self) :
return self.occurences
def addNewScore(self, newScore) :
oldScoreSum = self.averageScore * self.occurences
self.occurences = self.occurences + 1
self.averageScore = (oldScoreSum + newScore) / (self.occurences)
def printWordStatistic(self) :
print ("Word : "), self.keyword
print ("Occurences : "), self.occurences
print ("Average Score : ", self.occurences, "\n\n")
# Starting Training Phase
wordDictionary = {}
fileInstance = open("movieReviews.txt",'r')
fileText = fileInstance.read()
# Assuming, that each review is seperated by following delimeter
reviewSplits = fileText.split("$$EOD_REVIEW$$")
for review in reviewSplits :
review = review.strip()
if review == "" :
continue
# In each review, first line contains the score and the
# subsequent lines contains the text
lineSplits = review.split("\n")
score = float(lineSplits[0].strip())
for i in range(1, len(lineSplits)) :
# Splitting out the words in each line of the review
wordSplits = re.split("\t| ", lineSplits[i])
for word in wordSplits :
if word == "" :
continue
# If it is already present, then update the score and count
# Otherwise just add the new entry to the dictionary
if wordDictionary.has_key(word) :
wordStatistic = wordDictionary.get(word)
wordStatistic.addNewScore(score)
else :
wordStatistic = WordStatistic(word, score, 1)
wordDictionary[word] = wordStatistic
# Training Phase Completed
# To print the statistics of all words in the dictionary
def printAllWordStatistic(wordDictionary) :
for wordStatistic in wordDictionary.values() :
wordStatistic.printWordStatistic()
# To rate a review based on the training data
def calculateAverageOfReview(review) :
review.replace("\t", " ")
review.replace("\n", " ")
wordSplits = review.split(" ")
averageScore = 0.0
totalCount = 0;
for word in wordSplits :
if wordDictionary.has_key(word) :
averageScore += wordDictionary.get(word).getAverageScore()
totalCount = totalCount + 1
if totalCount != 0 :
return averageScore / totalCount
return -1
# User Review Input
while (True) :
print ("\nEnter a review, (enter empty-line to save) : ")
multiLines = []
while True:
line = raw_input()
if line:
multiLines.append(line)
else:
break
inputReview = '\n'.join(multiLines)
averageScore = calculateAverageOfReview(inputReview)
if averageScore != -1 :
if averageScore >= 2.50 :
print ("Positive Review")
else :
print ("Negative Review")
else :
print ("Unable to rate the review")
if raw_input("\nDo you want to continue ? (Y/N) : ") != "Y" :
print ("Quitting the session. Good Bye !")
exit()
So I am trying to read the reviews of movies and display the input of the rating but I get a conversion error below
Traceback (most recent call last):
File "C:/Users/Adil Ali/Documents/moviereview.py", line 44, in <module>
score = float(lineSplits[0].strip("\""))
ValueError: could not convert string to float: '1 A series of escapades demonstrating the adage that what is good for the goose is also good for the gander , some of which occasionally amuses but none of which amounts to much of a story .\t'
I tried to look up similar solutions to this issue but I could not find anything. Should I put a string in my strip function or do I need to change something up. Please let me know I am reading a text file off of movie reviews for this project
Error show you that in line
score = float(lineSplits[0].strip("\""))
you try to
score = float('1 A series of escapades ...')
You can't convert it to number. You have to get only 1 from this element. You could try split(" ")[0] to split line into list with elements and get first element - 1
score = float(lineSplits[0].strip("\"").split(" ")[0] )
Related
I have a .txt file with the names and information as below
John, 87, 64, 72, 79, 81, Yes
Steve, 32, 45, 29, 37, 34, No
I am trying to read from the .txt file and number each name so that the specific name can be selected by entering the number and information from the selected name can be edited.
I want to be able to choose from two options "to edit one of the amounts" or "change from No to Yes"
line_count = 0
with open("results.txt", 'r') as r_file:
for line in r_file:
results_info = line.split(",")
line_count += 1
print("\n" + str(line_count) + " " + " Name:\t" + results_info[0]
+ "\n " + "Subject_1:\t " + results_info[1]
+ "\n " + "Subject_2:\t" + results_info[2]
+ "\n " + "Subject_3:\t" + results_info[3]
+ "\n " + "Subject_4:\t" + results_info[4]
+ "\n " + "Subject_5:\t" + results_info[5]
+ "\n " + "Pass?\t" + results_info[6])
student_select = input("\nEnter the number of the student to edit: ")
I've gotten this far but I am stuck when it comes to the rest.
edit:
Never added subject 5 to the code
I would like to write the changes made back to the .txt file
Sorry, I don't think that I was clear enough. I'd like to display the list of all the students numbered. From there give the option to select a student by entering their number and then the option to edit a grade or change pass to yes.
So you have 3 tasks here: read data, manage user input, save data if something has been modified.
And the best structure to handle this is a dict of lists (I kept to your idea of numbering the rows, but you could easily have the student's name as key instead). The code could be something like this (some more checks are needed on user input, of course):
def edit_info(fname):
#read data
with open(fname, 'r') as r_file:
info = {}
for i,line in enumerate(r_file):
info[str(i)] = line.rstrip().split(', ')
#prompt user and update data
dirty = False
while True:
for k,vl in info.items():
print(f'{k:2} {vl[0]:10} {" ".join(vl[1:-1])} {vl[-1]:3}')
student = input('Enter Student number or X to exit... ')
if student == 'X':
break
if student in info:
while True:
subject = input('Enter Subject number, P to set pass to yes, or X to stop editing... ')
if subject == 'X':
break
if subject == 'P':
info[student][-1] = 'Yes'
dirty = True
elif '1' <= subject <= str(len(info[student])-1):
subject = int(subject)
newvalue = input(
f'Enter new value for Student {student} for Subject {subject} (currently {info[student][subject]})... ')
info[student][subject] = newvalue
dirty = True
#save data if needed
if dirty:
with open(fname, 'w') as w_file:
for vl in info.values():
w_file.write(', '.join(vl)+'\n')
print('Completed')
I'm new to Python too so I will do it in a stupid way sorry. Here is my code which changes all the info of the nth student as all the input you made
def select_student_to_edit(input_num):
all_result=[]
result0 = input('Edit name element: ')
result1 = " " + input('Edit 1th element: ')
result2 = " " + input('Edit 2th element: ')
result3 = " " + input('Edit 3th element: ')
result4 = " " + input('Edit 4th element: ')
result5 = " " + input('Edit 5th element: ')
result6 = " " + input('Edit Yes/No element: ') + "\n"
with open('test1', 'r+') as f:
if input_num>1:
for i in range(1,input_num):
all_result.append(f.readline()) #If input_num = n then read n-1 line before, add all of the to a all_result list contain all lines
#Start modifying the line(student) that you want to:
results_info = f.readline().split(",") #Split all info into list to modify
results_info[0] = result0
results_info[1] = result1
results_info[2] = result2
results_info[3] = result3
results_info[4] = result4
results_info[5] = result5
results_info[6] = result6
all_result.append(",".join(results_info)) #add modified studen to all_result list
#Add all rest lines into all_result list
rest_line = f.readlines()
for line in rest_line:
all_result.append(line)
print(all_result)
#Write all line(student) in all_result into the old file
with open('test1','w+') as f:
for line in all_result:
f.write(line)
select_student_to_edit(int(input('Enter the student you want to change info (nth): ')))
import re
with open("./teste/counter.txt", "r+") as count:
countread = count.read()
inputvar = input("Counting - write anything: ")
if countread == "":
print("Countread is ''None''. Adding to text file number ''1''.")
count.write('1')
else:
count.truncate(0)
countread = countread.replace(' ', '')
countplus = int(countread) + 1
print(countread)
count.write(str(countplus))
count.close()
I am trying to erase the file with count.truncate(0) but after it adds 1, and goes to 2 in my text file, at 3 I get the error:
ValueError: invalid literal for int() with base 10: '\x002'
For the line ''countplus = ...''
EDIT: By the way the ''countread replace'' was a try to fix this issue.
Fixed it with this
while 3>2:
with open("./teste/counter.txt", "r+") as count:
countread = count.read()
if countread == "":
countread = "0"
inputvar = input("Counting " + countread + " write anything: ")
if countread == "0":
count.write('1')
else:
countplus = int(countread) + 1
count.truncate(0)
count.seek(0)
countread = count.read()
count.write(str(countplus))
count.close()
here is my code:
import time
ed = input('Encrypt (e) or decrypt (d)? ')
chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
charsLen = len(chars)
def numberToStr(num):
s = ""
while num:
s = chars[num % charsLen] + s
num //= charsLen
return(s)
def strToNumber(numStr):
num = 0
for i, c in enumerate(reversed(numStr)):
num += chars.index(c) * (charsLen ** i)
return(num)
def enc():
key = input('What is your key? (Alphanumeric and space) ')
ID = int(input('What is your ID? (0-9, 3+ digits) '))
inp = int(strToNumber(input('What do you want to encrypt? ')))
keyAsNum = int(strToNumber(key))
enc.asint = inp ** 2
enc.asint = enc.asint * ID
enc.asint = enc.asint - keyAsNum
enc.astext = numberToStr(int(enc.asint))
return(enc)
def dec():
key = input('What is your key? (Alphanumeric and space) ')
ID = int(input('What is your ID? (0-9, 3+ digits) '))
inp = int(strToNumber(input('What do you want to decrypt? ')))
keyAsNum = int(strToNumber(key))
message = inp + keyAsNum
message = message // ID
message = math.sqrt(message)
message = numberToStr(message)
return(message)
if ed=='e':
crypt = enc()
print('crypt.asint:\n' + str(crypt.asint) + '\ncrypt.astext:\n' + crypt.astext)
elif ed=='d':
crypt = dec()
print(crypt)
time.sleep(10)
and here is the error:
File "stdin", line 5, in module
File "stdin", line 9, in dec
File "stdin", line 4, in numberToStr
TypeError: string indices must be integers
I cannot figure out why it is throwing this error and cannot find anything on google.
The traceback tells you exactly what is wrong. You are doing
s = chars[num % charsLen] + s
but you don't know for sure that num is an int because, previously, you do:
message = math.sqrt(message)
message = numberToStr(message)
What type does math.sqrt return?
The client's name is after the word "for" and before the opening parenthesis "(" that starts the proposal number. I need to extract the client name to use to look up the deal in a future step. What would be the easiest way to set this up? Using Zapier Extract Pattern or to Use Zapier Code in Python?
I have tried this and it did not work. It seemed promising though.
input_data
client = Reminder: Leruths has sent you a proposal for Business Name (#642931)
import regex
rgx = regex.compile(r'(?si)(?|{0}(.*?){1}|{1}(.*?)
{0})'.format('for', '('))
s1 = 'client'
for s in [s1]:
m = rgx.findall
for x in m:
print x.strip()
I have also tried this and it did not work.
start = mystring.find( 'for' )
end = mystring.find( '(' )
if start != -1 and end != -1:
result = mystring[start+1:end]
I am looking for Business Name to be returned in my example.
Fastest way:
start = client.find('for')
end = client.find('(')
result = client[start+4:end-1]
print(result)
With regex:
result = re.search(r' for (.*) [(]', client)
print(result.group(1))
There is probably a cleaner way to do this, but here is another solution without regex
client = "Reminder: Leruths has sent you a proposal for Business Name (#642931)"
cs = client.split(" ")
name = ""
append = False
for word in cs:
if "for" == word:
append = True
elif word.startswith("("):
append = False
if append is True and word != "for":
name += (word + " ")
name = name.strip()
print(name)
Another method:
client = "Reminder: Leruths has sent you a proposal for Business Name (#642931)"
cs = client.split(" ")
name = ""
forindex = cs.index("for")
for i in range(forindex+1, len(cs)):
if cs[i].startswith("("):
break
name += cs[i] + " "
name = name.strip()
print(name)
Running the code below gives:
Regex method took 2.3912417888641357 seconds
Search word by word method took 4.78193998336792 seconds
Search with list index method took 3.1756017208099365 seconds
String indexing method took 0.8496286869049072 seconds
Code to check the fastest to get the name over a million tries:
import re
import time
client = "Reminder: Leruths has sent you a proposal for Business Name (#642931)"
def withRegex(client):
result = re.search(r' for (.*) [(]', client)
return(result.group(1))
def searchWordbyWord(client):
cs = client.split(" ")
name = ""
append = False
for word in cs:
if "for" == word:
append = True
elif word.startswith("("):
append = False
if append is True and word != "for":
name += (word + " ")
name = name.strip()
return name
def searchWithListIndex(client):
cs = client.split(" ")
name = ""
forindex = cs.index("for")
for i in range(forindex+1, len(cs)):
if cs[i].startswith("("):
break
name += cs[i] + " "
name = name.strip()
return name
def stringIndexing(client):
start = client.find('for')
end = client.find('(')
result = client[start+4:end-1]
return result
wr = time.time()
for x in range(1,1000000):
withRegex(client)
wr = time.time() - wr
print("Regex method took " + str(wr) + " seconds")
sw = time.time()
for x in range(1,1000000):
searchWordbyWord(client)
sw = time.time() - sw
print("Search word by word method took " + str(sw) + " seconds")
wl = time.time()
for x in range(1,1000000):
searchWithListIndex(client)
wl = time.time() - wl
print("Search with list index method took " + str(wl) + " seconds")
si = time.time()
for x in range(1,1000000):
stringIndexing(client)
si = time.time() - si
print("String indexing method took " + str(si) + " seconds")
i am trying to get the exact word match from my file along with their line no.
like when i search for abc10 it gives me all the possible answers e.g abc102 abc103 etc
how can i limitize my code to only print what i commanded..
here is my code!
lineNo = 0
linesFound = []
inFile= open('rxmop.txt', 'r')
sKeyword = input("enter word ")
done = False
while not done :
pos = inFile.tell()
sLine = inFile.readline()
if sLine == "" :
done = True
break
if (sLine.find( sKeyword ) != -1):
print ("Found at line: "+str(lineNo))
tTuple = lineNo, pos
linesFound.append( tTuple )
lineNo = lineNo + 1
done = False
while not done :
command = int( input("Enter the line you want to view: ") )
if command == -1 :
done = True
break
for tT in linesFound :
if command == tT[0] :
inFile.seek( tT[1] )
lLine = inFile.readline()
print ("The line at position " + str(tT[1]) + "is: " + lLine)
"like when i search for abc10 it gives me all the possible answers e.g abc102 abc103 etc"
You split each record and compare whole "words" only.
to_find = "RXOTG-10"
list_of_possibles = ["RXOTG-10 QTA5777 HYB SY G12",
"RXOTG-100 QTA9278 HYB SY G12"]
for rec in list_of_possibles:
words_list=rec.strip().split()
if to_find in words_list:
print "found", rec
else:
print " NOT found", rec