Python HTML 字符串变量
Python HTML in string variable
我在 python 中使用 FPDF 通过函数生成 PDF 格式的发票。我使用字符串变量在 HTML 中构建了一个 table 并循环遍历一个列表,该列表具有条形码以填充 table 标签的行。
我遇到的问题是,当我尝试生成 PDF 时,我不断收到此 TypeError TypeError: string indices must be integers, not str
,这发生在进入循环并且 HTML 变量用行修改时HTML 字符串.
带有 table 标签的初始 HTML 变量如下所示:
html = """<table border="0" align="left" width="100%"><thead><tr><th align="left" width="25%">Code</th><th align="left" width="25%">Title</th><th align="left" width="25%">Variant</th><th align="left" width="25%">Price</th></tr></thead><tbody>"""
然后 for 循环看起来像这样,它将 <td>
行修改为 table 字符串变量:
for sku in _skus:
prod_info = finder(sku) #returns a JSON object from mongodb
html += "<tr><td>", str(prod_info['varsku']), "</td><td>", str(prod_info['prodtitle']), "</td><td>", str(prod_info['vartitle']), "</td><td>", str(prod_info['varprice']), "</td></tr>"
html += str("</tbody></table>")
pdf.write_html(html)
这就是 prod_info 从 finder()
过来后的这个阶段:
{"prodvendor": "Elpaso", "vartaxable": "false", "varquantity": "1", "varid": "33210590148", "varprice": "235.00", "prodtype": "Type1", "varcreated": "_varCreated", "prodtitle": "3 Way Cora Hols - Piol", "varsku": "102-10001-CRD-G19", "vartitle": "C P0 / Gk 19/23", "varoption": "C P0 / Gk 19/23", "_id": {"$oid": "58e77040da522f333c757852"}, "prodid": "9665780932"}
这是我得到 TypeError
的地方。
我试图将所有内容都转换为字符串以修补 "string" 变量,但我仍然遇到 TypeError。
任何帮助将不胜感激。
{{更新}}
def finder(_sku):
result = collection.find_one({"varsku":_sku})
if result == None:
return "None"
else:
return dumps(result)
如果这是您已经完成的事情,我很抱歉,但您收到的错误可能表明 prod_info 变量不是字典类型,而是字符串类型。
我建议使用 json.loads。
import json
prod_info = json.loads(prod_info)
这将允许访问属性。
您期望 prod_info
为 dict
,但实际上它是 str
,因此错误 "TypeError: string indices must be integers, not str" .
prod_info = finder(sku) #returns a JSON object from mongodb
Python 中没有 "JSON object" 这样的东西。您可以拥有普通的 Python 数据结构,例如 list
s 和 dict
s 或具有 JSON 序列化数据结构的字符串。在后一种情况下(TypeError
表明是这种情况),您不能直接操作这些数据结构。 JSON 必须先将字符串解码为本机数据结构。
你应该使用类似的东西:
prod_info = json.loads(finder(sku))
或者,更好的是,修改 finder
函数,使其在内部处理解码 JSON。
我发现了问题。这是我构造 html
字符串变量的方式。
当在变量中使用逗号 ,
时,表示变量被视为元组:字符串和变量之间的分隔符 ,
表示元组。
html += "<tr><td>", str(_code), "</td><td>", (_prodtitle), "</td>"
这意味着您正在组合字符串:
html += "<tr><td>"+str(_code)+"</td><td>"+str(_prodtitle)+"</td>
解决了TypeError: cannot concatenate 'str' and 'tuple' objects
的问题。
下面解决了最初的 TypeError: string indices must be integers, not str
问题。
prod_info = json.loads(finder(sku))
Thanks to this SO post 感谢@el.pescado 和@Ultcyber
我在 python 中使用 FPDF 通过函数生成 PDF 格式的发票。我使用字符串变量在 HTML 中构建了一个 table 并循环遍历一个列表,该列表具有条形码以填充 table 标签的行。
我遇到的问题是,当我尝试生成 PDF 时,我不断收到此 TypeError TypeError: string indices must be integers, not str
,这发生在进入循环并且 HTML 变量用行修改时HTML 字符串.
带有 table 标签的初始 HTML 变量如下所示:
html = """<table border="0" align="left" width="100%"><thead><tr><th align="left" width="25%">Code</th><th align="left" width="25%">Title</th><th align="left" width="25%">Variant</th><th align="left" width="25%">Price</th></tr></thead><tbody>"""
然后 for 循环看起来像这样,它将 <td>
行修改为 table 字符串变量:
for sku in _skus:
prod_info = finder(sku) #returns a JSON object from mongodb
html += "<tr><td>", str(prod_info['varsku']), "</td><td>", str(prod_info['prodtitle']), "</td><td>", str(prod_info['vartitle']), "</td><td>", str(prod_info['varprice']), "</td></tr>"
html += str("</tbody></table>")
pdf.write_html(html)
这就是 prod_info 从 finder()
过来后的这个阶段:
{"prodvendor": "Elpaso", "vartaxable": "false", "varquantity": "1", "varid": "33210590148", "varprice": "235.00", "prodtype": "Type1", "varcreated": "_varCreated", "prodtitle": "3 Way Cora Hols - Piol", "varsku": "102-10001-CRD-G19", "vartitle": "C P0 / Gk 19/23", "varoption": "C P0 / Gk 19/23", "_id": {"$oid": "58e77040da522f333c757852"}, "prodid": "9665780932"}
这是我得到 TypeError
的地方。
我试图将所有内容都转换为字符串以修补 "string" 变量,但我仍然遇到 TypeError。
任何帮助将不胜感激。
{{更新}}
def finder(_sku):
result = collection.find_one({"varsku":_sku})
if result == None:
return "None"
else:
return dumps(result)
如果这是您已经完成的事情,我很抱歉,但您收到的错误可能表明 prod_info 变量不是字典类型,而是字符串类型。
我建议使用 json.loads。
import json
prod_info = json.loads(prod_info)
这将允许访问属性。
您期望 prod_info
为 dict
,但实际上它是 str
,因此错误 "TypeError: string indices must be integers, not str" .
prod_info = finder(sku) #returns a JSON object from mongodb
Python 中没有 "JSON object" 这样的东西。您可以拥有普通的 Python 数据结构,例如 list
s 和 dict
s 或具有 JSON 序列化数据结构的字符串。在后一种情况下(TypeError
表明是这种情况),您不能直接操作这些数据结构。 JSON 必须先将字符串解码为本机数据结构。
你应该使用类似的东西:
prod_info = json.loads(finder(sku))
或者,更好的是,修改 finder
函数,使其在内部处理解码 JSON。
我发现了问题。这是我构造 html
字符串变量的方式。
当在变量中使用逗号 ,
时,表示变量被视为元组:字符串和变量之间的分隔符 ,
表示元组。
html += "<tr><td>", str(_code), "</td><td>", (_prodtitle), "</td>"
这意味着您正在组合字符串:
html += "<tr><td>"+str(_code)+"</td><td>"+str(_prodtitle)+"</td>
解决了TypeError: cannot concatenate 'str' and 'tuple' objects
的问题。
下面解决了最初的 TypeError: string indices must be integers, not str
问题。
prod_info = json.loads(finder(sku))
Thanks to this SO post 感谢@el.pescado 和@Ultcyber