I have to figure out how to count the number of cars each country sold, preferab
ID: 3718744 • Letter: I
Question
I have to figure out how to count the number of cars each country sold, preferably using Django (python). If the website for this information was "www.website.com/123", what would be the code for this?
id,import country,model,make,sold by,sale price 1,Jordan,S-Class,Mercedes-Benz,Martha Mustard,18104 2, Indonesia,, Chrysler,Giavani Middleweek, 21381 3,China,Xtra, Toyota, Lambert Baraja, 12812 4, Sweden, Phantom, Rolls-Royce, Sutherland Jakubczyk,14732 5, Indonesia,Monte Carlo, Chevrolet,Trixie Felderer,18744 6, France, Samurai,Suzuki,Sinclair Danielot,14871 7, Taiwan,riolet,Audi, Luelle Lowensohn, 13442 8, Thailand, Range Rover, Land Rover, Cassandra Creaney,11748 9,Argentina,323,Mazda, Danice Larwood,17387Explanation / Answer
Notes: Basically for every line that is read from text file, you need to maintain the totals of each country that you come across and in the end display the results.
To run the below code,
step1:modify/replace your views.py to the code I provided.
step2:Have a file mydata.txt placed with data that is depicted below in your web project folder.
step3:Make sure you modify your urls.py to call 'hello'. My website link was http://127.0.0.1:8000/123/
compiler version: Python 2.7, Django 1.11.12
VIEWS.PY
from django.http import HttpResponse
from django.shortcuts import render
import collections
def hello(request):
text = readfiledata()
return HttpResponse(text)
def readfiledata():
text = ""
linenumber=0
cars_list = list()
with open("mydata.txt", "r") as filestream:
for line in filestream:
currentline = line.split(",")
tab = " "
if len(currentline) == 6:
text = text + currentline[0] + tab + currentline[1] + tab + currentline[2] + tab + currentline[3]+ tab + currentline[4] + tab + currentline[5]
text = text + "<br/>"
if (linenumber > 0):
cars_list.append(currentline[1])
linenumber=linenumber+1
c = collections.Counter(cars_list)
for word, count in c.items():
text = text + "<br/> COUNTRY " + word + "=" + str(count)
return text
INPUT : URLS.PY (configuration marked in bold below. notice the text 123 in the url)
from django.conf.urls import url, include
from django.contrib.auth import views as auth_views
from myapp import views as myapp_views
# This two if you want to enable the Django Admin: (recommended)
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^123/', myapp_views.hello, name='hello'),
]
INPUT : MYDATA.TXT
id,import_country,model,make,sold_by,sale_price
1,Jordan,S-Class,Mercedes-Benz,Martha Mustard,18104
2,Indonesia,,Chrysler,Martha Mustard,21381
3,China,Xtra,Toyota,Martha Mustard,12812
4,Sweden,Phantom,Rolls-Royce,Martha Mustard,14732
5,Indonesia,Monte,Chevrolet,Martha Mustard,18744
6,France,Samurai,Suzuki,Martha Mustard,14871
7,Taiwan,riolet,Audi,Martha Mustard,13442
8,Thailand,Range Rover,Land Rover,Martha Mustard,11748
9,Argentina,323,Mazda,Martha Mustard,17387
10,Jordan,S-Class,Mercedes-Benz,Martha Mustard,18104
11,Jordan,S-Class,Mercedes-Benz,Martha Mustard,18104
12,Indonesia,,Chrysler,Martha Mustard,21381
13,China,Xtra,Toyota,Martha Mustard,12812
14,Sweden,Phantom,Rolls-Royce,Martha Mustard,14732
15,Indonesia,Monte,Chevrolet,Martha Mustard,18744
16,France,Samurai,Suzuki,Martha Mustard,14871
17,Taiwan,riolet,Audi,Martha Mustard,13442
18,Thailand,Range Rover,Land Rover,Martha Mustard,11748
19,Thailand,323,Mazda,Martha Mustard,17387
OUTPUT :
id import_country model make sold_by sale_price
1 Jordan S-Class Mercedes-Benz Martha Mustard 18104
2 Indonesia Chrysler Martha Mustard 21381
3 China Xtra Toyota Martha Mustard 12812
4 Sweden Phantom Rolls-Royce Martha Mustard 14732
5 Indonesia Monte Chevrolet Martha Mustard 18744
6 France Samurai Suzuki Martha Mustard 14871
7 Taiwan riolet Audi Martha Mustard 13442
8 Thailand Range Rover Land Rover Martha Mustard 11748
9 Argentina 323 Mazda Martha Mustard 17387
10 Jordan S-Class Mercedes-Benz Martha Mustard 18104
11 Jordan S-Class Mercedes-Benz Martha Mustard 18104
12 Indonesia Chrysler Martha Mustard 21381
13 China Xtra Toyota Martha Mustard 12812
14 Sweden Phantom Rolls-Royce Martha Mustard 14732
15 Indonesia Monte Chevrolet Martha Mustard 18744
16 France Samurai Suzuki Martha Mustard 14871
17 Taiwan riolet Audi Martha Mustard 13442
18 Thailand Range Rover Land Rover Martha Mustard 11748
19 Thailand 323 Mazda Martha Mustard 17387
COUNTRY Indonesia=4
COUNTRY France=2
COUNTRY Sweden=2
COUNTRY China=2
COUNTRY Thailand=3
COUNTRY Jordan=3
COUNTRY Taiwan=2
COUNTRY Argentina=1