I have an assignment to make a web scraper using BeautifulSoup.
There are certain functions defined in the code.
How do I pass the bs4.element.ResultSet to another function and extract relevant data in the form of a dictionary as bs4.element.ResultSet is passed as a python list file.
Here is the code I have been able to write so far, comments are included for further information.
def fetchWebsiteData(url_website):
"""Fetches rows of tabular data from given URL of a website with data excluding table headers.
Parameters
----------
url_website : str
URL of a website
Returns
-------
bs4.element.ResultSet
"""
web_page_data = ''
####
req = requests.get(url_website)
soup= BeautifulSoup(req.text,'html.parser')
web_page_data = soup.find_all('tbody')
####
return web_page_data
The other function that I am using:
def fetchVaccineDoses(web_page_data):
"""Fetch the Vaccine Doses available from the Web-page data and provide Options to select the respective Dose.
Parameters
----------
web_page_data : bs4.element.ResultSet
All rows of Tabular data fetched from a website excluding the table headers
Returns
-------
dict
Dictionary with the Doses available and Options to select, with Key as 'Option' and Value as 'Command'
Example
-------
>>> url_website = "https://www.mooc.e-yantra.org/task-spec/fetch-mock-covidpage"
>>> web_page_data = fetchWebsiteData(url_website)
>>> print(fetchVaccineDoses(web_page_data))
{'1': 'Dose 1', '2': 'Dose 2'}
"""
vaccine_doses_dict = {}
####
for dose in web_page_data:
dose = dose.find_all('td', class_="dose_num")
for k in dose:
#print (k.next_element)
if (k.next_element) == 1:
vaccine_doses_dict['1'] = "Dose 1"
else:
vaccine_doses_dict['2'] = "Dose 2"
####
return vaccine_doses_dict
And:
def fetchAgeGroup(web_page_data, dose):
"""Fetch the Age Groups for whom Vaccination is available from the Web-page data for a given Dose
and provide Options to select the respective Age Group.
Parameters
----------
web_page_data : bs4.element.ResultSet
All rows of Tabular data fetched from a website excluding the table headers
dose : str
Dose available for Vaccination and its availability for the Age Groups
Returns
-------
dict
Dictionary with the Age Groups (for whom Vaccination is available for a given Dose) and Options to select,
with Key as 'Option' and Value as 'Command'
Example
-------
>>> url_website = "https://www.mooc.e-yantra.org/task-spec/fetch-mock-covidpage"
>>> web_page_data = fetchWebsiteData(url_website)
>>> print(fetchAgeGroup(web_page_data, '1'))
{'1': '18+', '2': '45+'}
>>> print(fetchAgeGroup(web_page_data, '2'))
{'1': '18+', '2': '45+'}
"""
age_group_dict = {}
####
####
return age_group_dict
PS-> I am pretty new to programming and learning so please mind the bad code.
PPS->This sort of thing is what I want to make
So, I modifed your code a little bit:
Example 01 - Not what Questioner wants! [Without Output]
Example 02 - Customized what Questioner want to have [With Output]
Example 03 - How I do the work [With Output]
Example 01:
from bs4 import BeautifulSoup
import requests
def fetchWebsiteData(url_website):
web_page_data = ''
####
req = requests.get(url_website)
soup= BeautifulSoup(req.text,'html.parser')
####
####Changes here
return soup
def fetchVaccineDoses(web_page_data):
dose_count = 0
vaccine_doses_dict = {}
####
doses = web_page_data.find_all('td', class_="dose_num")
####Changes here too
for dose in doses:
vaccine_doses_dict[dose_count] = "Dose " + dose.text
dose_count = dose_count + 1
####
return vaccine_doses_dict
####Changes here too - Complete your empty function with code -
####I hope it is your output what you want to see.
def fetchAgeGroup(web_page_data, doses):
age_count = 0
####
ages = web_page_data.find_all('td', class_="age")
for age in ages:
doses[age_count] = doses[age_count] + " | Age " + age.text
age_count = age_count + 1
####
return doses
url_website = "https://www.mooc.e-yantra.org/task-spec/fetch-mock-covidpage"
web_page_data = fetchWebsiteData(url_website)
VaccineDoses = fetchVaccineDoses(web_page_data)
Ages = fetchAgeGroup(web_page_data, VaccineDoses)
####Ages element 0 can delete because it contains no numbers
####It is only the table header
del Ages[0]
print(Ages)
Example 02:
So, here the result code of your picture example.
from bs4 import BeautifulSoup
import requests
def fetchWebsiteData(url_website):
web_page_data = ''
####
req = requests.get(url_website)
soup= BeautifulSoup(req.text,'html.parser')
####
return soup
def fetchVaccineDoses(web_page_data):
dose_count = 0
vaccine_doses_dict = {}
####
doses = web_page_data.find_all('td', class_="dose_num")
for dose in doses:
if "Dose " + dose.text in vaccine_doses_dict.keys():
vaccine_doses_dict["Dose " + dose.text] += 1
else:
vaccine_doses_dict["Dose " + dose.text] = 1
dose_count = dose_count + 1
####
#### Table header remove
del vaccine_doses_dict["Dose Dose"]
return vaccine_doses_dict
def fetchAgeGroup(web_page_data, doseNum):
age_dict = {}
age_count = 0
####
lines = web_page_data.find_all('tr')
for line in lines:
if age_count == 0:
##Ignore table header
age_count += 1
continue
if fetchOnlyNumber(line.find("td", class_="dose_num").text) == doseNum:
current_age = line.find("td", class_="age").text
if current_age in age_dict.keys():
age_dict[current_age] += 1
else:
age_dict[current_age] = 1
####
return age_dict
def fetchOnlyNumber(inputStr):
number = 0
for sChar in inputStr:
if sChar.isdigit():
number = (number * 10) + int(sChar)
else:
return -1
return number
def fetchUserInput():
### Integer only
while True:
print("Choose: ", end="")
usrInput = fetchOnlyNumber( input() )
if usrInput > 0:
return usrInput
else:
if usrInput == 0:
###Exit
return 0
else:
print("Wrong input! Only numbers are allowed!")
url_website = "https://www.mooc.e-yantra.org/task-spec/fetch-mock-covidpage"
web_page_data = fetchWebsiteData(url_website)
VaccineDoses = fetchVaccineDoses(web_page_data)
indexVacc = 1
print(">>> Select the Dose of Vaccination:")
for VaccineDose, amount in VaccineDoses.items():
print("{0} :- {1} [Amount: {2}]".format(indexVacc, VaccineDose, amount))
indexVacc += 1
print("0 :- Exit")
doseNum = fetchUserInput()
if doseNum == 0:
exit(0)
print("<<< Dose Selected: {0}".format(doseNum))
print(">>> Select the Age Group:")
indexAge = 0
Ages = fetchAgeGroup(web_page_data, doseNum)
for age, amount in Ages.items():
print("{0} :- {1} [Amount: {2}]".format(indexAge, age, amount))
indexAge += 1
Output of Example 02:
>>> Select the Dose of Vaccination:
1 :- Dose 1 [Amount: 33]
2 :- Dose 2 [Amount: 42]
0 :- Exit
Choose: 2
<<< Dose Selected: 2
>>> Select the Age Group:
0 :- 45+ [Amount: 29]
1 :- 18+ [Amount: 13]
Example 03:
from bs4 import BeautifulSoup
import requests
def fetchWebsiteData(url_website):
web_page_data = ''
####
req = requests.get(url_website)
soup= BeautifulSoup(req.text,'html.parser')
####
return soup
def fetchAllPossibleData(web_page_data):
data_collaction_dict = {}
doses = {}
data_index = 0
### FOR LOOP BEGINS
for tr in web_page_data.find_all('tr'):
if data_index == 0:
#table header ignore
data_index += 1
continue
hospital_name = tr.find("td", class_="hospital_name").text
state_name = tr.find("td", class_="state_name").text
district_name = tr.find("td", class_="district_name").text
vaccine_name = tr.find("td", class_="vaccine_name").text
dose_num = tr.find("td", class_="dose_num").text
age = tr.find("td", class_="age").text
### dict = { Key: Value, Key: Value, ... }
data_collaction_dict[data_index] = {
"Hospital": hospital_name,
"State": state_name,
"District": district_name,
"Vaccine": vaccine_name,
"Dose": dose_num,
"Age": age
}
### Count "Dose 1" and "Dose 2"
if dose_num in doses:
doses[dose_num] += 1
else:
doses[dose_num] = 1
data_index += 1
### FOR LOOP ENDS
data_collaction_dict["AmountDose"] = doses
return data_collaction_dict
def fetchOnlyNumber(inputStr):
number = 0
for sChar in inputStr:
if sChar.isdigit():
number = (number * 10) + int(sChar)
else:
return -1
return number
def fetchUserInput():
### Integer only
while True:
print("Choose: ", end="")
usrInput = fetchOnlyNumber( input() )
if usrInput > 0:
return usrInput
else:
if usrInput == 0:
###Exit
return 0
else:
print("Wrong input! Only numbers are allowed!")
url_website = "https://www.mooc.e-yantra.org/task-spec/fetch-mock-covidpage"
web_page_data = fetchWebsiteData(url_website)
whole_data = fetchAllPossibleData(web_page_data)
dose_index = 1
print(">>> Select the Dose of Vaccination:")
for dose, amount in whole_data["AmountDose"].items():
print("{0} :- Dose {1} [Amount: {2}]".format(dose_index, dose, amount))
dose_index += 1
print("0 :- Exit")
doseNum = fetchUserInput()
if doseNum == 0:
exit(0)
print("<<< Dose Selected: {0}".format(doseNum))
print(">>> Select the Age Group:")
for key in whole_data.keys():
if key == "AmountDose":
continue
if int(whole_data[key]["Dose"]) == doseNum:
print( "Hospital: {0} | Vaccine: {1} | Dose: {2} | Age: {3}".format(
whole_data[key]["Hospital"],
whole_data[key]["Vaccine"],
whole_data[key]["Dose"],
whole_data[key]["Age"]))
print('-' * 100)
Output of Example 03:
>>> Select the Dose of Vaccination:
1 :- Dose 1 [Amount: 33]
2 :- Dose 2 [Amount: 42]
0 :- Exit
Choose: 1
<<< Dose Selected: 1
>>> Select the Age Group:
Hospital: Apollo Hospital | Vaccine: Covaxin | Dose: 1 | Age: 45+
--------------------------------------------------------------------------------
Hospital: Springedge Care | Vaccine: Covaxin | Dose: 1 | Age: 18+
--------------------------------------------------------------------------------
Hospital: West Valley Medical Center | Vaccine: Covaxin | Dose: 1 | Age: 45+
--------------------------------------------------------------------------------
Hospital: Zenlife Clinic | Vaccine: Covaxin | Dose: 1 | Age: 18+
--------------------------------------------------------------------------------
Hospital: Family Wellness Center | Vaccine: Covishield | Dose: 1 | Age: 18+
--------------------------------------------------------------------------------
Hospital: Tranquil Valley Hospital Center | Vaccine: Covaxin | Dose: 1 | Age: 45+
--------------------------------------------------------------------------------
Hospital: SevenHills | Vaccine: Covishield | Dose: 1 | Age: 18+
--------------------------------------------------------------------------------
.... more output with python script Exapmle 03 - but I think above is enough....
Related
I have the following data in a text file.
Intium II,2.8,24,128
Celerisc I,1.6,32,256
Ethloan III,2.6,32,128
Powerup II,1.9,64,512
Mitduo III,3.1,24,128
I am doing the following:
Allocating points to each processor
Points will be awarded to each processor as follows:
Clock speed is less than 2 GHz, award 10 points.
Clock speed is between 2 GHz and 3 GHz inclusive, award 20 points.
Clock speed is greater than 3 GHz, award 30 points.
Data Bus Points
1 point for each line on the data bus e.g. 24 bit data bus, award 24 points.
Cache Size Points
1 point for each whole 10 Kb of cache e.g. 128Kb cache, award 12 points.
(128/10 = 12ยท8, which should be rounded down to 12)
The output from the program should be similar to the following:
John Doe your order code is JD3f
Processor Points
Intium II 56
Celerisc I 67
Ethloan III 64
Powerup II 125
Mitduo III 66
Here is my code
import random
import string
def getDetails():
forename = input("Enter first name: ")
surname = input("Enter last name: ")
number = random.randint(0,9)
character = random.choice(string.ascii_letters).lower()
code = (forename[:1] + str(surname[:1]) + str(number) + str(character))
return forename, surname, code
def readProcessorDetails():
processorName = []*5
clockSpeed = []*5
dataBusWidth = []*5
cacheSize = []*5
file = open("processors.txt","r")
for line in file:
data = line.split(",")
processorName.append(data[0])
clockSpeed.append(float(data[1]))
dataBusWidth.append(data[2])
cacheSize.append(data[3])
input("file read successfully.. Press any key to continue")
return processorName, clockSpeed, dataBusWidth, cacheSize
def allocatePoints(clockSpeed, dataBusWidth, cacheSize):
proPoints = 0.0
processorPoints = []
for counter in range(len(clockSpeed)):
if clockSpeed[counter] < 2.0 or dataBusWidth[counter] == 24 or dataBusWidth[counter] == 128:
proPoints = proPoints + 10 + 24 + 12
processorPoints.append(proPoints)
elif clockSpeed[counter] > 2.0 or clockSpeed[counter] <= 3.0 or dataBusWidth[counter] == 24 or dataBusWidth[counter] == 128:
proPoints = proPoints + 20 + 24 + 12
processorPoints.append(proPoints)
elif clockSpeed[counter] > 3.0 or dataBusWidth[counter] == 24 or dataBusWidth[counter] == 128:
proPoints = proPoints + 30 + 24 + 12
processorPoints.append(proPoints)
return processorPoints
def display(processorName, processorPoints):
print(f"Processor \t Points")
for counter in range(len(processorPoints)):
print(f"{processorName[counter]}\t {processorPoints[counter]}\n")
def main():
forename, surname, code = getDetails()
processorName, clockSpeed, dataBusWidth, cacheSize = readProcessorDetails()
processorPoints = allocatePoints(clockSpeed, dataBusWidth, cacheSize)
print()
print(forename + " " + surname + " your order code is " + code)
print()
display(processorName, processorPoints)
main()
Here is my code output
Enter first name: John
Enter last name: Doe
file read successfully.. Press any key to continue
John Doe your order code is JD8z
Processor Points
Intium II 56.0
Celerisc I 102.0
Ethloan III 158.0
Powerup II 204.0
Mitduo III 260.0
I am not sure what I am doing wrong in my allocatePoints() function where I have used complex if statements and running total to add processor points.
Consider keeping all of the data in one list, rather than trying to coordinate multiple lists. This lets you avoid the unPythonic "for x in range(len(x))" construct, and iterate through the list directly.
I also fixed your root problem, which is that you weren't resetting the points to zero for every new processor.
import random
import string
def getDetails():
forename = input("Enter first name: ")
surname = input("Enter last name: ")
number = random.randint(0,9)
character = random.choice(string.ascii_letters).lower()
code = (forename[:1] + str(surname[:1]) + str(number) + str(character))
return forename, surname, code
def readProcessorDetails():
pdata = []
for line in open("processors.txt"):
data = line.split(",")
pdata.append( [data[0], float(data[1]), int(data[2]), int(data[3])] )
return pdata
def allocatePoints(pdata):
for row in pdata:
points = 0
if row[1] < 2.0:
points += 10
elif row[1] <= 3.0:
points += 20
else:
points += 30
points += row[2]
points += row[3] // 10
row.append( points )
def display(pdata):
print("Processor\tPoints")
for row in pdata:
print(f"{row[0]}\t{row[-1]}")
def main():
forename, surname, code = getDetails()
processorData = readProcessorDetails()
allocatePoints(processorData)
print()
print(forename + " " + surname + " your order code is " + code)
print()
display(processorData)
main()
I grabbed few lines of data from a url. I am trying to compare one largest value of a type against another. Im missing something as I failed to extract it properly.
import requests, re, time
from bs4 import BeautifulSoup
from selenium import webdriver
trim = re.compile(r'[^\d,.]+')
driver = webdriver.Chrome('chromedriver.exe')
url = "https://poocoin.app/rugcheck/0x8076c74c5e3f5852037f31ff0093eeb8c8add8d3/top-holders"
driver.get(url)
time.sleep(8)
soup = BeautifulSoup(driver.page_source, 'lxml')
t = soup.find('table', class_='table table-bordered table-condensed text-small')
bigwallet = 0
bigcontract = 0
contractbalance = 0
walletbalance = 0
for row in soup.select("tr:has(td)")[:10]:
addr = row.find_all("td")[0].text[0:]
trans = row.find_all("td")[4].text[0:]
bal = row.find_all("td")[5].text[0:].strip()
tbal = trim.sub('', bal).replace(",", "")
tbal = float(tbal)
wtype = row.find_all("td")[2].text[0:].strip()
if (str(wtype) == "Contract"):
contractbalance = float(tbal)
else:
walletbalance = float(tbal)
if (walletbalance) > (contractbalance):
bigwallet = walletbalance
else:
bigcontract = contractbalance
bigwallet = bigwallet
bigcontract = bigcontract
print(" {} {:<20} {:<5} {:>5} ".format(addr, bal, trans, wtype))
print (" Largest Contract: {} Largest Wallet: {} ".format(bigwallet, bigcontract))
driver.quit()
Current Output: #-- current problematic output
0x9adc6fb78cefa07e13e9294f150c1e8c1dd566c0 12,704,309,869,844.8669 325911 Contract
0xc95063d946242f26074a76c8a2e94c9d735dfc78 7,745,539,348,064.8244 11 Wallet
0x0000000000000000000000000000000000000001 423,229,310,780,801.1327 159 Contract
0xff3dd404afba451328de089424c74685bf0a43c9 15,407,439,439,186.9579 389180 Contract
0x86b695aaa2600668cec754c7827357626b188054 10,311,345,756,789.1980 9 Wallet
0x010b86c90654905611b31dbfaf5883ba616b9833 0.0000 1 Wallet
#-- problematic part of my code
Largest Contract: 0 Largest Wallet: 15407439439186.957
Wanted Output:
Largest Contract: 423,229,310,780,801.1327 Largest Wallet: 10,311,345,756,789.1980
0x9adc6fb78cefa07e13e9294f150c1e8c1dd566c0 12,704,309,869,844.8669 325911 Contract
0xc95063d946242f26074a76c8a2e94c9d735dfc78 7,745,539,348,064.8244 11 Wallet
0x0000000000000000000000000000000000000001 423,229,310,780,801.1327 159 Contract
0xff3dd404afba451328de089424c74685bf0a43c9 15,407,439,439,186.9579 389180 Contract
0x86b695aaa2600668cec754c7827357626b188054 10,311,345,756,789.1980 9 Wallet
0x010b86c90654905611b31dbfaf5883ba616b9833 0.0000 1 Wallet
You can collect all those values to respective list and then take the max value among those.
bigwallet = [] # List of wallet values
bigcontract = [] # List of contract values
for row in soup.select("tr:has(td)")[:10]:
addr = row.find_all("td")[0].text[0:]
trans = row.find_all("td")[4].text[0:]
bal = row.find_all("td")[5].text[0:].strip()
tbal1 = trim.sub('', bal).replace(",", "")
tbal = float(tbal1)
wtype = row.find_all("td")[2].text[0:].strip()
if (str(wtype) == "Contract"):
bigcontract.append(tbal) # Append to bigcontract if its a contact one else append to bigwallet.
else:
bigwallet.append(tbal)
#print(" {} {:<20} {:<5} {:>5} ".format(addr, bal, trans, wtype))
print (" Largest Contract: {} Largest Wallet: {} ".format(max(bigcontract),max(bigwallet))) # print max of bigcontact and big wallet
driver.quit()
Largest Contract: 423234543343603.1 Largest Wallet: 10311473240313.781
Instead of all those comparisons, you can directly use the built-in max() function to get the maximum values of Contract and Wallet.
Here is the code:
import requests, re, time
from bs4 import BeautifulSoup
from selenium import webdriver
trim = re.compile(r'[^\d,.]+')
driver = webdriver.Chrome('chromedriver.exe')
url = "https://poocoin.app/rugcheck/0x8076c74c5e3f5852037f31ff0093eeb8c8add8d3/top-holders"
driver.get(url)
time.sleep(8)
soup = BeautifulSoup(driver.page_source, 'lxml')
t = soup.find('table', class_='table table-bordered table-condensed text-small')
bigwallet = 0
bigcontract = 0
contractbalance = 0
walletbalance = 0
for row in soup.select("tr:has(td)")[:10]:
addr = row.find_all("td")[0].text[0:]
trans = row.find_all("td")[4].text[0:]
bal = row.find_all("td")[5].text[0:].strip()
tbal = trim.sub('', bal).replace(",", "")
tbal = float(tbal)
wtype = row.find_all("td")[2].text[0:].strip()
if (str(wtype) == "Contract"):
contractbalance = float(tbal)
bigcontract = max(bigcontract, contractbalance)
else:
walletbalance = float(tbal)
bigwallet = max(bigwallet, walletbalance)
print(" {} {:<20} {:<5} {:>5} ".format(addr, bal, trans, wtype))
print (" Largest Contract: {} Largest Wallet: {} ".format(bigcontract, bigwallet))
driver.quit()
0x9adc6fb78cefa07e13e9294f150c1e8c1dd566c0 12,705,728,079,579.6744 325911 Contract
0xc95063d946242f26074a76c8a2e94c9d735dfc78 7,745,635,776,415.0881 11 Wallet
0xa8736b9585a01d6dcc1b6e2fc9dc208552c34b58 20,000,000,001,566.1322 6 Contract
0x0000000000000000000000000000000000000001 423,234,579,788,211.4459 159 Contract
0xff3dd404afba451328de089424c74685bf0a43c9 15,372,532,667,134.5367 389418 Contract
0x86b695aaa2600668cec754c7827357626b188054 10,311,474,128,231.8625 9 Wallet
0x010b86c90654905611b31dbfaf5883ba616b9833 0.0000 1 Wallet
0x0add13cde4c61734f46e245b1b5fe3afe9b6bc29 0.0000 1 Wallet
0xc5becfc3771400ac372c3b1a0712899c72f75792 39.1084 1 Wallet
0xcc64ea842fcde4283cf239259f7462ef809c44fd 149,028.9837 36 Wallet
Largest Contract: 423234579788211.44 Largest Wallet: 10311474128231.863
For you to solve my problem I think you have to run it on your own. My problem is in this line print(Customer_Name, "has spent", Customers[Customer_Name]['TotalAmount'], "in total")
For you to understand better. For example here, It says Michael has spent 60.0 in total when it should be Michael has spent 20.0 in total
import datetime
import uuid # GET A RANDOM ID FOR THE CUSTOMER
from csv import DictWriter
from datetime import date # GET CURRENT DATE AND TOMORROW DATE
import os
def openFile(): # STARTS HERE
try:
os.startfile('data_entered.csv')
except Exception as e:
print(str(e))
# OPEN FILE WHEN USER(EMPLOYEE) WANTS TO
def closeFile():
try:
os.system('TASKKILL /F /IM notepad.exe')
except Exception as e:
print(str(e)) # ENDS HERE
file = open('CustomerNames.txt', 'a')
file1 = open('Orders_Per_Users.txt', 'a')
file2 = open('data_entered.csv', 'a')
ORDERS_FROM_EMPLOYEE = 0
x = -1
length = 0
Total_Amount = 0.0
Customer_List = []
Address_List = []
My_List = []
today = datetime.date.today()
Today_Key = date.toordinal(date.today())
Today_Date = date.today()
Tomorrow_Date = datetime.date.today() + datetime.timedelta(days=1)
Customers = {}
Dates = {}
print("Welcome to our coffee shop!")
print("Login")
# EMPLOYEE LOGIN PROCESS STARTS
UserLogin = {"Employee1": "coffeeshop1", "Employee2": "coffeeshop2", "Employee3": "coffeeshop3"}
username = password = ''
while True:
username = input("Username: ")
password = input("Password: ")
if (username in UserLogin) and (UserLogin[username] == password):
print("Login Successful")
break
else:
print("Invalid Login. Try again")
# EMPLOYEE LOGIN PROCESS ENDS
# PROCESS AFTER ORDER PLACEMENT STARTS
print("Current Date is: {}".format(Today_Date))
process1 = True
process2 = True
while process1:
while process2:
x += 1
Customer_Name = input("Customer's Name:")
Customer_Address = input("Customer's Address:")
if Customer_Name in Customer_List and Customer_Address in Address_List:
First_Index = Customer_List.index(Customer_Name)
Second_Index = Address_List.index(Customer_Address)
if Customer_Name == Customer_List[First_Index]:
Customer_List.pop(First_Index)
x -= 1
Address_List.append(Customer_Address)
Customer_List.append(Customer_Name)
if Today_Key not in Dates:
Dates[Today_Key] = {}
if Customer_Name not in Dates[Today_Key]:
Dates[Today_Key][Customer_Name] = 1
else:
Dates[Today_Key][Customer_Name] += 1
if Customer_Name in Customers:
Customers[Customer_Name]['Orders'] += 1
Customers[Customer_Name]['TotalAmount'] = Total_Amount
else:
Customers[Customer_Name] = {}
Customers[Customer_Name]['Name'] = Customer_Name
Customers[Customer_Name]['Address'] = Customer_Address
Customers[Customer_Name]['ID'] = uuid.uuid1()
Customers[Customer_Name]['Orders'] = 1
Customers[Customer_Name]['TotalAmount'] = 0
Order_Price = float(input("Total amount of order:"))
ORDERS_FROM_EMPLOYEE += 1
print(Customer_Name, "has ordered {} time(s)".format(Customers[Customer_Name]['Orders']))
Total_Amount = Order_Price + Total_Amount
if Tomorrow_Date == Today_Date: # WHEN THIS IS TRUE, IT MEANS THAT THE DATE CHANGED
print("Total amount of orders today is:{} ".format(Total_Amount)) # NUMBER OF ORDERS IN ONE SPECIFIC DAY
answer1 = input("Send another order? (Y/N)").lower()
if Customers[Customer_Name]['Orders'] == 1:
print("This is the first time", Customer_Name, "orders")
Customers[Customer_Name]['TotalAmount'] = Order_Price
else: # TOTAL AMOUNT OF ALL ORDERS DELIVERED
print(Customer_Name, "has spent", Customers[Customer_Name]['TotalAmount'], "in total")
process2 = answer1 == "y"
LengthCustomersList = len(Customer_List)
length += 1
if int(length) == int(LengthCustomersList):
process1 = False
file1.write(username + " has placed {} orders today".format(ORDERS_FROM_EMPLOYEE))
for i in Customer_List:
file.write(i + '\n')
csv_writer = DictWriter(open('data_entered.csv', 'a'),
fieldnames=['Customer Name', 'Customer Address', 'Customer ID', 'Total Orders',
'Total Amount'])
csv_writer.writeheader()
for customer_name in Customers.keys():
csv_writer.writerows(
[{'Customer Name': Customers[customer_name]['Name'],
'Customer Address': Customers[customer_name]['Address'],
'Customer ID': Customers[customer_name]['ID'],
'Total Orders': Customers[customer_name]['Orders'],
'Total Amount': Customers[customer_name]['TotalAmount']}])
openFile()
file.close()
file1.close()
file2.close()
I have tried adding a Customers[Customer_Name]['Order_Price'] but it did not work
Your issue is with the Total_Amount variable. Its value is kept from the last iteration and assigned to the next customer. You need to retrieve it from your Customers dictionary and then update the value in the dictionary after.
if Customer_Name in Customers:
Customers[Customer_Name]['Orders'] += 1
# Customers[Customer_Name]['TotalAmount'] = Total_Amount # This is where the error was
else:
Customers[Customer_Name] = {}
Customers[Customer_Name]['Name'] = Customer_Name
Customers[Customer_Name]['Address'] = Customer_Address
Customers[Customer_Name]['ID'] = uuid.uuid1()
Customers[Customer_Name]['Orders'] = 1
Customers[Customer_Name]['TotalAmount'] = 0
Total_Amount = Customers[Customer_Name]['TotalAmount'] # retrieve value here
Order_Price = float(input("Total amount of order:"))
ORDERS_FROM_EMPLOYEE += 1
print(Customer_Name, "has ordered {} time(s)".format(Customers[Customer_Name]['Orders']))
Total_Amount = Order_Price + Total_Amount
Customers[Customer_Name]['TotalAmount'] = Total_Amount # update value here
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] )
In my if/elif/else statement my program keeps returning only the else where it should be 1 B and 2 Cs.
document = open ("ClassNameGrades.txt","r")
content = document.read().splitlines()
As = 0
Bs = 0
Cs = 0
Ds = 0
Fs = 0
for line in content:
split = line.split (", ")
className = split [0]
facultyName = split [1:1]
studentName= split [2:2]
grade = split [3:]
if str(grade) == ("A"):
As = As + 1
elif str(grade) == ("B"):
Bs = Bs + 1
elif str(grade) == ("C"):
Cs = Cs + 1
elif str(grade) == ("D"):
Ds = Ds + 1
else:
Fs = Fs + 1
print (str(As)+(" students got As."))
print (str(Bs)+(" students got Bs."))
print (str(Cs)+(" students got Cs."))
print (str(Ds)+(" students got Ds."))
print (str(Fs)+(" students got Fs."))
document.close()
The results for this shows as:
0 students got As.
0 students got Bs.
0 students got Cs.
0 students got Ds.
3 students got Fs.
You are comparing a list item to a string, which is why your if conditions never evaluate to True. Instead of using the list slice notation split[2:2] which returns a list, you should simply fetch the items from the list using the list index notation.
document = open ("ClassNameGrades.txt","r")
content = document.read().splitlines()
As = 0
Bs = 0
Cs = 0
Ds = 0
Fs = 0
for line in content:
split = line.split(", ")
className = split[0]
facultyName = split[1]
studentName= split[2]
grade = split[3]
if str(grade) == "A":
As = As + 1
elif str(grade) == "B":
Bs = Bs + 1
elif str(grade) == "C":
Cs = Cs + 1
elif str(grade) == "D":
Ds = Ds + 1
else:
Fs = Fs + 1
print(str(As)+(" students got As."))
print(str(Bs)+(" students got Bs."))
print(str(Cs)+(" students got Cs."))
print(str(Ds)+(" students got Ds."))
print(str(Fs)+(" students got Fs."))
document.close()