0

So I have written a procedure to format a list that I have in Python to be written in an HTML form as JavaScript. it looks like this

from django.template.defaultfilters import slugify def js_format(cars): new_cars = [] for car in cars: car = str(slugify(car))+'|'+car new_cars.append(car) return new_cars dodge = ["Avenger","Caliber","Caravan","Challenger","Colt","D150","D250","D350","Dakota","Dart","Daytona","Durango","Durango Hybrid","Dynasty","Grand Caravan","Intrepid","Journey","Magnum","Monaco","Neon","Nitro","Omni","Raider","Ram 1500","Ram 2500","Ram 3500","Ram 50","Ram Van","Ram Wagon","Ramcharger","Shadow","Sprinter","SRT Viper","Spirit","Stealth","Stratus","Viper","W150","W250","W350"] 

when I use the procedure on the 'dodge' list...

>>> js_format(dodge) ['avenger|Avenger', 'caliber|Caliber', 'caravan|Caravan', 'challenger|Challenger', 'colt|Colt', 'd150|D150', 'd250|D250', 'd350|D350', 'dakota|Dakota', 'dart|Dart', 'daytona|Daytona', 'durango|Durango', 'durango-hybrid|Durango Hybrid', 'dynasty|Dynasty', 'grand-caravan|Grand Caravan', 'intrepid|Intrepid', 'journey|Journey', 'magnum|Magnum', 'monaco|Monaco', 'neon|Neon', 'nitro|Nitro', 'omni|Omni', 'raider|Raider', 'ram-1500|Ram 1500', 'ram-2500|Ram 2500', 'ram-3500|Ram 3500', 'ram-50|Ram 50', 'ram-van|Ram Van', 'ram-wagon|Ram Wagon', 'ramcharger|Ramcharger', 'shadow|Shadow', 'sprinter|Sprinter', 'srt-viper|SRT Viper', 'spirit|Spirit', 'stealth|Stealth', 'stratus|Stratus', 'viper|Viper', 'w150|W150', 'w250|W250', 'w350|W350'] 

Everything is formatted the way I would like except I need each item in the list to be in double quotes. I don't know how to change the quotation marks in python because python recognizes single quoted and double strings equally. Any help would be appreciated.

Thank you for viewing my question

1
  • Do you need one javascript literal, or do you need a list of javascript literals? In other words, will the output of js_format be assigned to one javascript variable? Commented Nov 30, 2012 at 21:26

3 Answers 3

5

Use json.dumps() to create Javascript literals:

def js_format(cars): new_cars = ['{0!s}|{1}'.format(slugify(car)), car) for car in cars] return json.dumps(new_cars) 

This will generate a full JS list with properly quoted strings:

>>> js_format(dodge) '["avenger|Avenger", "caliber|Caliber", "caravan|Caravan", "challenger|Challenger", "colt|Colt", "d150|D150", "d250|D250", "d350|D350", "dakota|Dakota", "dart|Dart", "daytona|Daytona", "durango|Durango", "durango-hybrid|Durango Hybrid", "dynasty|Dynasty", "grand-caravan|Grand Caravan", "intrepid|Intrepid", "journey|Journey", "magnum|Magnum", "monaco|Monaco", "neon|Neon", "nitro|Nitro", "omni|Omni", "raider|Raider", "ram-1500|Ram 1500", "ram-2500|Ram 2500", "ram-3500|Ram 3500", "ram-50|Ram 50", "ram-van|Ram Van", "ram-wagon|Ram Wagon", "ramcharger|Ramcharger", "shadow|Shadow", "sprinter|Sprinter", "srt-viper|SRT Viper", "spirit|Spirit", "stealth|Stealth", "stratus|Stratus", "viper|Viper", "w150|W150", "w250|W250", "w350|W350"]' 
Sign up to request clarification or add additional context in comments.

Comments

2

Inside of your for loop, change your assignment to the following:

car = '"' + str(slugify(car)) + '|' + car + '"' 

Alternatively, you could use string formatting:

car = '"{0}|{1}"'.format(str(slugify(car)), car) 

It is possible I'm misinterpreting this though, do you want js_format() to return a JSON string? If so, use your current function but import json at the top of the file and change your return statement to return json.dumps(new_cars).

1 Comment

Format is much better solution then strings concatenation
0

If you need your string to contain double quotes — add them manually:

car = '"%s|%s"' % (slugify(car), ca) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.