I am trying to filter cities from a json file and showup first five cities from the filtered list with a help of Django app.
But when I'm trying to return list element in HTTPResponse line by line, it gives me everything in single line.
views.py file
from django.http import HttpResponse
import re
import os
from django.conf import settings
def index(request):
a= str(request.GET['city'])
with open(os.path.join(settings.MEDIA_ROOT, 'all_city_info.json')) as f:
data = f.read()
lis=sorted(x[9:-1] for x in re.findall('"text": "'+re.escape(a)+'[\s\S]+?"', data))
return HttpResponse('\n'.join([str(x) for x in lis[:5]]))
Input is being taken from the user in city variable like this: http://127.0.0.1:8000/api/?city=New
Result which i get:
New York Mills|Minnesota|United States New York Mills|New York|United States New York|New York|United States New York|Norfolk|United Kingdom
Please advise, what shall i do so that I get cities in line on browser when I return HTTPResponse.
Thanks!
Sample data, if anybody want's to take a look-
{"City": {"values": [{"text": "Abee|Alberta|Canada", "state": "AB", "id": 21774}, {"text": "Acadia Valley|Alberta|Canada", "state": "AB", "id": 21775}, {"text": "Acme|Alberta|Canada", "state": "AB", "id": 21776}, {"text": "Airdrie|Alberta|Canada", "state": "AB", "id": 21777}, {"text": "Alderson|Alberta|Canada", "state": "AB", "id": 21778}, {"text": "Alix|Alberta|Canada", "state": "AB", "id": 21779}, {"text": "Alliance|Alberta|Canada", "state": "AB", "id": 21780}, {"text": "Andrew|Alberta|Canada", "state": "AB", "id": 21781}, {"text": "Ardmore|Alberta|Canada", "state": "AB", "id": 21782}, {"text": "Ardrossan|Alberta|Canada", "state": "AB", "id": 21783}, {"text": "Ashmont|Alberta|Canada", "state": "AB", "id": 21784}, {"text": "Athabasca|Alberta|Canada", "state": "AB", "id": 21785}, {"text": "Atikameg|Alberta|Canada", "state": "AB", "id": 21786}, {"text": "Atmore|Alberta|Canada", "state": "AB", "id": 21787}, {"text": "Avenir|Alberta|Canada", "state": "AB", "id": 21788}, {"text": "Balzac|Alberta|Canada", "state": "AB", "id": 21789}, {"text": "Banff|Alberta|Canada", "state": "AB", "id": 21790}, {"text": "Barons|Alberta|Canada", "state": "AB", "id": 21791}, {"text": "Barrhead|Alberta|Canada", "state": "AB", "id": 21792}, {"text": "Bashaw|Alberta|Canada", "state": "AB", "id": 21793}, {"text": "Bassano|Alberta|Canada", "state": "AB", "id": 21794}, {"text": "Beaumont|Alberta|Canada", "state": "AB", "id": 21795}, {"text": "Beaverlodge|Alberta|Canada", "state": "AB", "id": 21796}, {"text": "Beiseker|Alberta|Canada", "state": "AB", "id": 21797}, {"text": "Bellevue|Alberta|Canada", "state": "AB", "id": 21798}, {"text": "Bellis|Alberta|Canada", "state": "AB", "id": 21799}, {"text": "Benalto|Alberta|Canada", "state": "AB", "id": 21800}, {"text": "Bentley|Alberta|Canada", "state": "AB", "id": 21801}, {"text": "Bergen|Alberta|Canada", "state": "AB", "id": 21802}, {"text": "Berwyn|Alberta|Canada", "state": "AB", "id": 21803}, {"text": "Big Valley|Alberta|Canada", "state": "AB", "id": 21804}, {"text": "Bilby|Alberta|Canada", "state": "AB", "id": 21805}, {"text": "Bittern Lake|Alberta|Canada", "state": "AB", "id": 21806}, {"text": "Black Diamond|Alberta|Canada", "state": "AB", "id": 21807}, {"text": "Blackfalds|Alberta|Canada", "state": "AB", "id": 21808}, {"text": "Blackie|Alberta|Canada", "state": "AB", "id": 21809}, {"text": "Blairmore|Alberta|Canada", "state": "AB", "id": 21810}, {"text": "Blue Ridge|Alberta|Canada", "state": "AB", "id": 21811}, {"text": "Bluesky|Alberta|Canada", "state": "AB", "id": 21812}, {"text": "Bluffton|Alberta|Canada", "state": "AB", "id": 21813}, {"text": "Bon Accord|Alberta|Canada", "state": "AB", "id": 21814}, {"text": "Bonnyville|Alberta|Canada", "state": "AB", "id": 21815}, {"text": "Bowden|Alberta|Canada", "state": "AB", "id": 21816}, {"text": "Bow Island|Alberta|Canada", "state": "AB", "id": 21817}, {"text": "Boyle|Alberta|Canada", "state": "AB", "id": 21818}, {"text": "Brampton|Alberta|Canada", "state": "AB", "id": 21819}]}}
jsonmodule?