I'm trying to use string formatting to convert incoming CSV to XML. I'm getting the following error:
ValueError: unsupported format character '"' (0x22) at index 36
I understand that I'm using an " somewhere I shouldn't but can't find where that is. The " inside the template should all be encapsulated inside the """ ... """ and I can't see any elsewhere in the script.
Could anyone suggest where I've gone wrong?
import csv
import sys
def csvDict(csvRow):
dict = {'Name': csvRow[0], 'p1t': csvRow[1], 'p1l': csvRow[2], 'p2t': csvRow[3], 'p2l': csvRow[4],
'outputWidth': csvRow[5], 'sourceTop': csvRow[6], 'sourceLeft': csvRow[7], 'sourceWidth': csvRow[8],
'sourceHeight': csvRow[9]}
return dict
# Get CSV File from the argument
csvFile = csv.reader(open(sys.argv[1], 'rt'))
# Convert CSV Into list for Processing
csvList = list(csvFile)
# Setup XML Variables Dictionary
outputVars = csvDict(csvList[0])
# Confirm Dictionary contains the right data
print outputVars
# XML Format Template
mapTemplate = """<map type="map2dpanel" name="%(Name)" width="%(outputWidth)" >
<point id="1" top="%(p1t)" left="%(p1l)" /><point id="2" top="%(p2t)" left="%(p2l)" />
<source image="current source" top="%(sourceTop)" left="%(sourceLeft)" width="%(sourceWidth)" height="%(sourceHeight)" />
</map>
"""
print mapTemplate % outputVars
You forgot to specify the type of placeholder. Python expects %(name)s
or %(name)d
or any of the other supported types .
Instead, Python finds "
to be the next character, which is not a valid format character:
name="%(Name)"
# -------^
Since you are reading the values from a CSV file, they'll all be strings; add s
characters to your template placeholders:
mapTemplate = """\
<map type="map2dpanel" name="%(Name)s" width="%(outputWidth)s" >
<point id="1" top="%(p1t)s" left="%(p1l)s" /><point id="2" top="%(p2t)s" left="%(p2l)s" />
<source image="current source" top="%(sourceTop)s" left="%(sourceLeft)s" width="%(sourceWidth)s" height="%(sourceHeight)s" />
</map>
"""
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.