[英]Value Error unsupported format character '"' when using % string formatting
I'm trying to use string formatting to convert incoming CSV to XML. 我正在尝试使用字符串格式将传入的CSV转换为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 . Python需要%(name)s
或%(name)d
或任何其他受支持的类型 。
Instead, Python finds "
to be the next character, which is not a valid format character: 取而代之的是,Python发现下一个字符"
不是有效的格式字符:
name="%(Name)"
# -------^
Since you are reading the values from a CSV file, they'll all be strings; 由于您是从CSV文件中读取值,因此它们都是字符串。 add s
characters to your template placeholders: 将s
字符添加到模板占位符:
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>
"""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.