1

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}]}}

4
  • 1
    wouldn't it be easier to parse the json string at some point using the json module? Commented Aug 20, 2015 at 12:17
  • That was increasing the execution time of the program so I did it this way. Commented Aug 20, 2015 at 12:28
  • It might increase the execution time but it's the right thing to do. So unless you actually can measure/proove the runtime is a real problem please don't use such ugly hacks. Commented Aug 20, 2015 at 14:42
  • @BlackJack The execution time was a problem actually, Earlier it was 0.35 secs to filter out cities from a file of 12 mb but my manager wanted me to make it below 0.05 and I tried regular expression, it came to be 0.03 sec with it. Commented Aug 20, 2015 at 15:39

1 Answer 1

1

\n does not mean anything in your HTML response. Use the <br> tag to separate lines instead.

Sign up to request clarification or add additional context in comments.

1 Comment

@Sanchit Alternatively you could set the content type to text/plain, then the '\n' has meaning again. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.